Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I combine two columns with offset data?

My dataset contains two columns with data that are offset - something like:

col1<-c("a", "b", "c", "d", "ND", "ND", "ND", "ND")
col2<-c("ND", "ND", "ND", "ND", "e", "f", "g", "h")
dataset<-data.frame(cbind(col1, col2))

I would like to combine those two offset columns into a single column that contains the letters a through h and nothing else.

Something like the following is what I'm thinking, but rbind is not the right command:

dataset$combine<-rbind(dataset$col1[1:4], dataset$col2[5:8])
like image 685
Luke Avatar asked Jan 20 '26 11:01

Luke


1 Answers

What about:

sel2 <- col2!="ND"
col1[sel2] <- col2[sel2]
> col1
[1] "a" "b" "c" "d" "e" "f" "g" "h"
like image 108
Ari B. Friedman Avatar answered Jan 23 '26 02:01

Ari B. Friedman



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!