[R] Prefix
David Winsemius
dwinsemius at comcast.net
Mon Nov 9 01:38:28 CET 2015
> On Nov 8, 2015, at 4:05 PM, Val <valkremk at gmail.com> wrote:
>
> HI all,
>
> DF <- read.table(textConnection(" X1 X2 X3 TIME
> Alex1 0 0 1960
> Alexa 0 0 1920
> Abbot 0 0 0
> Smith Alex1 Alexa 2012
> Carla Alex1 0 1996
> Jacky Smith Abbot 2013
> Jack 0 Jacky 2014
> Almo Jack Carla 2015 "),header = TRUE)
I would suggests using stringsAsFactors=FALSE
>
>
> I want to add the time variable as prefix to the first column (X1)
> and I did it as follow,
>
> DF$X4 <- as.character(paste(DF$TIME,DF$X1 ,sep="_"))
> DF
>
> All names in column two (X1) and three (X3) are in column one. so I just
> want bring that prefix to column three and two, as well but I could not do
> that one.
>
> Here is the final output that I would like to have.
>
> X1 X2 X3
> 1960_Alex 0 0
> 1920_Alexa 0 0
> 0_Abbot 0 0
> 2012_Smith 1960_Alex 1920_Alexa
> 1996_Carla 1960_Alex 0
> 2013_Jacky 2012_Smith 0_Abbot
> 2014_Jack 0 2013_Jacky
> 2015_Almo 2014_Jack 1996_Carla
If you follow my suggestion above, tehn these two lines produce vectors that may be of some use:
> paste(DF$TIME[match(DF$X2,DF$X1)], DF$X2, sep="_")
[1] "NA_0" "NA_0" "NA_0" "1960_Alex1" "1960_Alex1"
[6] "2012_Smith" "NA_0" "2014_Jack"
> paste(DF$TIME[match(DF$X3,DF$X1)], DF$X3, sep="_")
[1] "NA_0" "NA_0" "NA_0" "1920_Alexa" "NA_0"
[6] "0_Abbot" "2013_Jacky" “1996_Carla"
>
>
> Your help is appreciated in advance
>
> [[alternative HTML version deleted]]
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> 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.
David Winsemius
Alameda, CA, USA
More information about the R-help
mailing list