Hi, My data.frame "A" has FID like this FID a a b b b c c d d d d Now my second data.frame "B" has age value for a, b, c, d like FID Age a 5 b 7 c 9 d 3 How can search for the Age column in "B" and replace the values in "A" so that my new "A" looks like this FID Age a 5 a 5 b 7 b 7 b 7 c 9 c 9 d 3 d 3 d 3 d 3 Thanks you [[alternative HTML version deleted]]
On Aug 19, 2012, at 5:56 PM, Sapana Lohani wrote:> Hi, > > > My data.frame "A" has FID like this > > FID > > a > > a > b > b > b > c > c > d > d > d > d > > Now my second data.frame "B" has age value for a, b, c, d like > > FID Age > > a 5 > > b 7 > c 9 > d 3 > > How can search for the Age column in "B" and replace the values in > "A" so that my new "A" looks like this? merge Perhaps merge(A, B) # although the ambiguities inherent in posting only screen output instead of the greatly-to-be-preferred output of dput() makes this a guess.> > FID Age > > a 5 > a 5 > b 7 > b 7 > b 7 > c 9 > c 9 > d 3 > d 3 > d 3 > d 3 > > Thanks you > > [[alternative HTML version deleted]]And posting html is a deprecated format for rhelp.> > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.(Console output does not qualify as reproducible.) -- David Winsemius, MD Alameda, CA, USA
Hi, Try this: A<-data.frame(FID=c("a","a","b","b","c","c","d","d","d","d")) B<-read.table(text=" FID Age a????? 5 b????? 7 c????? 9 d????? 3 ",sep="",header=TRUE) library(plyr) join(A,B,type="inner") Joining by: FID ?? FID Age 1??? a?? 5 2??? a?? 5 3??? b?? 7 4??? b?? 7 5??? c?? 9 6??? c?? 9 7??? d?? 3 8??? d?? 3 9??? d?? 3 10?? d?? 3 A.K. ----- Original Message ----- From: Sapana Lohani <lohani.sapana at ymail.com> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Sunday, August 19, 2012 8:56 PM Subject: [R] relating data in two data frames Hi, My data.frame "A" has FID like this FID a a b b b c c d d d d Now my second data.frame "B" has age value for a, b, c, d like FID Age a????? 5 b????? 7 c????? 9 d????? 3 How can search for the Age column in "B" and replace the values in "A" so that my new "A" looks like this FID Age a????? 5 a????? 5 b????? 7 b????? 7 b????? 7 c????? 9 c????? 9 d????? 3 d????? 3 d????? 3 d????? 3 Thanks you ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Hello everyone, My dataframe (Soil Type) looks something like this Granitic Hills 16-20 PZ Loamy Upland 16-20 PZ Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ Loamy Upland 16-20 PZ I want to separate them at "/", but when there is just one type, I want "NA" in the second column, How can I do that ?? Thank you [[alternative HTML version deleted]]
try this:> x <- scan(text = "Granitic Hills 16-20 PZ+ Loamy Upland 16-20 PZ + Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ + Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ + Loamy Upland 16-20 PZ", what = '', sep = '\n') Read 5 items> x[1] "Granitic Hills 16-20 PZ" [2] "Loamy Upland 16-20 PZ" [3] "Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ" [4] "Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ" [5] "Loamy Upland 16-20 PZ"> y <- strsplit(x, '/') > # now put NA is there is only one entry > y <- lapply(y, function(.line){+ if (length(.line) == 1) .line <- c(.line, NA) # add NA + .line + })> do.call(rbind, y)[,1] [,2] [1,] "Granitic Hills 16-20 PZ" NA [2,] "Loamy Upland 16-20 PZ" NA [3,] "Sandy Loam Upland 12-16 PZ " " Sandy Loam, Deep 12-16 PZ" [4,] "Loamy Upland 12-16 PZ " " Sandy Loam Upland 12-16 PZ" [5,] "Loamy Upland 16-20 PZ" NA>On Tue, Aug 21, 2012 at 1:54 AM, Sapana Lohani <lohani.sapana at ymail.com> wrote:> Hello everyone, > > > My dataframe (Soil Type) looks something like this > > Granitic Hills 16-20 PZ > Loamy Upland 16-20 PZ > Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ > Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ > Loamy Upland 16-20 PZ > > I want to separate them at "/", but when there is just one type, I want "NA" in the second column, How can I do that ?? > > Thank you > [[alternative HTML version deleted]] > > ______________________________________________ > R-help at r-project.org mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, reproducible code.-- Jim Holtman Data Munger Guru What is the problem that you are trying to solve? Tell me what you want to do, not how you want to do it.
How about this? read.table(text = "Granitic Hills 16-20 PZ Loamy Upland 16-20 PZ Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ Loamy Upland 16-20 PZ", header = FALSE, sep = "/", fill = TRUE, na.strings = "", strip.white = TRUE) ## V1 V2 ## 1 Granitic Hills 16-20 PZ <NA> ## 2 Loamy Upland 16-20 PZ <NA> ## 3 Sandy Loam Upland 12-16 PZ Sandy Loam, Deep 12-16 PZ ## 4 Loamy Upland 12-16 PZ Sandy Loam Upland 12-16 PZ ## 5 Loamy Upland 16-20 PZ <NA> -- Noia Raindrops noia.raindrops at gmail.com
Hi, Try this: dat1 <- readLines(textConnection("Granitic Hills 16-20 PZ ?Loamy Upland 16-20 PZ ?Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ ?Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ ?Loamy Upland 16-20 PZ"))? dat3<-strsplit(dat1,"/") ?data.frame(col1=do.call(rbind,lapply(dat3,`[`,1)),col2=do.call(rbind,lapply(dat3,`[`,2))) # ? ? ? ? ? ? ? ? ? ? ? ? ?col1??????????????????????? col2 #1????? Granitic Hills 16-20 PZ??????????????????????? <NA> #2??????? Loamy Upland 16-20 PZ??????????????????????? <NA> #3? Sandy Loam Upland 12-16 PZ??? Sandy Loam, Deep 12-16 PZ #4?????? Loamy Upland 12-16 PZ?? Sandy Loam Upland 12-16 PZ #5??????? Loamy Upland 16-20 PZ??????????????????????? <NA>? A.K. ----- Original Message ----- From: Sapana Lohani <lohani.sapana at ymail.com> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Tuesday, August 21, 2012 1:54 AM Subject: [R] irregular splits in dataframe Hello everyone, My dataframe (Soil Type) looks something like this Granitic Hills 16-20 PZ Loamy Upland 16-20 PZ Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ Loamy Upland 16-20 PZ I want to separate them at "/", but when there is just one type, I want "NA" in the second column, How can I do that ?? Thank you ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Hi, Slight modification to my earlier code to make it more simple. dat1 <- readLines(textConnection("Granitic Hills 16-20 PZ ?Loamy Upland 16-20 PZ ?Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ ?Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ ?Loamy Upland 16-20 PZ")) dat3<-strsplit(dat1,"/") data.frame(do.call(rbind,lapply(dat3,`[`,c(1,2)))) #??????????????????????????? X1????????????????????????? X2 #1????? Granitic Hills 16-20 PZ??????????????????????? <NA> #2??????? Loamy Upland 16-20 PZ??????????????????????? <NA> #3? Sandy Loam Upland 12-16 PZ??? Sandy Loam, Deep 12-16 PZ #4?????? Loamy Upland 12-16 PZ?? Sandy Loam Upland 12-16 PZ #5??????? Loamy Upland 16-20 PZ??????????????????????? <NA> A.K. ----- Original Message ----- From: Sapana Lohani <lohani.sapana at ymail.com> To: "r-help at r-project.org" <r-help at r-project.org> Cc: Sent: Tuesday, August 21, 2012 1:54 AM Subject: [R] irregular splits in dataframe Hello everyone, My dataframe (Soil Type) looks something like this Granitic Hills 16-20 PZ Loamy Upland 16-20 PZ Sandy Loam Upland 12-16 PZ / Sandy Loam, Deep 12-16 PZ Loamy Upland 12-16 PZ / Sandy Loam Upland 12-16 PZ Loamy Upland 16-20 PZ I want to separate them at "/", but when there is just one type, I want "NA" in the second column, How can I do that ?? Thank you ??? [[alternative HTML version deleted]] ______________________________________________ R-help at r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.