I have three columns of data, and would like to create a new fourth column that contains either the value from column one or column two dependent on what is in column three. Any ideas? Thank you.
Asked
Active
Viewed 3,088 times
0
-
This could be a much more interesting question if you provided a minimal reproducible example showing an exact circumstance of what you mean - http://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example?rq=1 – thelatemail Jan 19 '15 at 01:15
-
Not sure in what way the dependency is, but my recent question may be of some use to you http://stackoverflow.com/questions/27997916/selecting-value-based-on-variable-name-of-data-frame-column-in-r . In my case the dependency is very explicit, name of the column to use is in the determinant column (i.e. your third column) – Ricky Jan 19 '15 at 03:45
2 Answers
4
Easily done via ifelse()
:
# generate example data
dat <- data.frame(x1= rnorm(100), x2= rnorm(100), x3= sample(c(1,2), 100, replace=TRUE))
# use x3 values to determine x4
dat$x4 <- ifelse(dat$x3 ==1, dat$x1, dat$x2)

alexwhitworth
- 4,839
- 5
- 32
- 59
0
Using @Alex's data, suppose if the column3 value represents the column numbers or column names of the first two columns, this could be done with "row/column" indexing to get the position of the element and use [
to subset the values. For example in the below, "dat$x3" is numeric "1,2" values. By using cbind(seq_len(nrow(dat)...)
, we are providing info about the location of the value in the "dat".
dat$x4 <- dat[cbind(seq_len(nrow(dat)), dat$x3)]
But, if the "x3" is some other value, for example "xyz", "uvw", and wants to select "x1" value if "xyz" and "x2" for "uvw"
indx <- cbind(seq_len(nrow(dat1)), setNames(1:2,
c('xyz', 'uvw'))[as.character(dat1$x3)])
dat1$x4 <- dat1[-3][indx]
Or
dat1$x4 <- dat1$x2
dat1$x4[dat1$x3=='xyz'] <- dat1$x1[dat1$x3=='xyz']
data
set.seed(24)
dat <- data.frame(x1= rnorm(100), x2= rnorm(100), x3= sample(1:2, 100, replace=TRUE))
set.seed(24)
dat1 <- data.frame(x1= rnorm(100), x2= rnorm(100), x3= sample(c('xyz', 'uvw'), 100, replace=TRUE))

akrun
- 874,273
- 37
- 540
- 662