[R] Prefix
Val
valkremk at gmail.com
Tue Nov 10 02:09:52 CET 2015
HI Dennis and David
Thank you very much! I really appreciate that.
Now I am half way from my final goal. At the end what I would like is the
following output.
New_X1 New_X2 New_X3 Old X1
1 0 0 0_Abbot
2 0 0 1920_Alexa
3 0 0 1960_Alex1
4 1 2 2012_Smith
5 1 0 1996_Carlo
6 4 1 2013_Jacky
7 0 6 2014_Jack
8 7 5 2015_Almo
The idea here is that to order the individual based on time and creating a
unique individual and indexing it. At the end I want them merge the
original X1 values to the new values
I tried but I did not reach to my final goal.
v1 <- DF$X4 <- as.character(paste(DF$TIME,DF$X1 ,sep="_"))
v2 <- paste(DF$TIME[match(DF$X2,DF$X1)], DF$X2, sep="_")
v3 <- paste(DF$TIME[match(DF$X3,DF$X1)], DF$X3, sep="_")
v11 <- matrix(v1, nrow = length(v1), byrow = TRUE)
v12 <- matrix(v2, nrow = length(v2), byrow = TRUE)
v13 <- matrix(v3, nrow = length(v3), byrow = TRUE)
v4 <- cbind(v11,v12,v13)
v5 <- as.matrix(v4)
v5 <- replace(x, x == "NA_0", 0) # repalce the "NA_0"
v6 <- sort(unique(as.vector(v5)))
My problem is how to merge the original unique Old_X1 values to newly
created values in the matrix
Thank you.
On Mon, Nov 9, 2015 at 2:49 PM, Dennis Murphy <djmuser at gmail.com> wrote:
> Hi:
>
> Here's a way to do it with apply().
>
> 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, stringsAsFactors =
> FALSE)
>
>
> refcol <- with(DF, paste(TIME, X1, sep = "_"))
>
> f <- function(x)
> {
> r <- DF[, 1] # reference column for names
> u <- match(x, r) # match positions of names in vector x with those
> in r
> v <- refcol[u] # substitute original names with those in refcol
> v[is.na(v)] <- "0" # set NAs in v to "0"
> v
> }
>
> apply(DF[, grep("^X", names(DF))], 2, f) # grep() selects out
> columns starting with "X"
>
> Dennis
>
> On Sun, 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 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
> >
> >
> > 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.
>
[[alternative HTML version deleted]]
More information about the R-help
mailing list