I have lots of sets of variables like this:
Var1 Var2
"Asian" NA
NA "Black"
"White" NA
I would like to conveniently get them into this form:
Race
"Asian"
"Black"
"White"
I have been trying something like:
Race <- ifelse(is.na(Var1), Var2, Var1)
But this converts the values into numbers for the levels, and the numbers don't match up (e.g., that yields 1, 1, 2
). Is there a convenient way to do this (ideally with short, self-explanatory code)? (You can get out of this with as.character
, but there has to be a better way.)
How do I concatenate two columns in R? To concatenate two columns you can use the <code>paste()</code> function. For example, if you want to combine the two columns A and B in the dataframe df you can use the following code: <code>df['AB'] <- paste(df$A, df$B)</code>.
You can assign the same value to multiple variables by using = consecutively. This is useful, for example, when initializing multiple variables to the same value. It is also possible to assign another value into one after assigning the same value.
With an intermediate conversion via as.character
:
Assuming this is your data:
dat <- data.frame(Var1=c("Asian",NA,"White"),Var2=c(NA,"Black",NA))
do.call(pmax,c(lapply(dat,as.character),na.rm=TRUE))
#[1] "Asian" "Black" "White"
If you need to work on a particular subset you can do:
do.call(pmax,c(lapply(dat[c("Var1","Var2")],as.character),na.rm=TRUE))
An alternative not requiring as.character
would be:
dat[cbind(1:nrow(dat),max.col(!is.na(dat)))]
#[1] "Asian" "Black" "White"
What about this solution?:
ind <- apply(df, 1, function(x) which(!is.na(x)))
df[cbind(seq_along(ind), ind)]
[1] "Asian" "Black" "White"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With