简体   繁体   中英

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])

What about:

sel2 <- col2!="ND"
col1[sel2] <- col2[sel2]
> col1
[1] "a" "b" "c" "d" "e" "f" "g" "h"

Use sapply and an anonymous function:

dataset[sapply(dataset, function(x) x != "ND")]
# [1] "a" "b" "c" "d" "e" "f" "g" "h"
dataset$combine <- dataset[sapply(dataset, function(x) x != "ND")]
dataset
#   col1 col2 combine
# 1    a   ND       a
# 2    b   ND       b
# 3    c   ND       c
# 4    d   ND       d
# 5   ND    e       e
# 6   ND    f       f
# 7   ND    g       g
# 8   ND    h       h

使用grep查找匹配的元素并选择它们:

c(col1[grep("^[a-h]$",col1)],col2[grep("^[a-h]$",col2)])

Yet another way, using mapply and gsub :

 within(dataset, combine <- mapply(gsub, pattern='ND', replacement=col2, x=col1))
#   col1 col2 combine
# 1    a   ND       a
# 2    b   ND       b
# 3    c   ND       c
# 4    d   ND       d
# 5   ND    e       e
# 6   ND    f       f
# 7   ND    g       g
# 8   ND    h       h

Per your comment to @Andrie's answer, this will also preserve NA rows.

Another point of view:

transform(dataset, 
          combine=dataset[apply(dataset, 2, function(x) x %in% letters[1:8])])
  col1 col2 combine
1    a   ND       a
2    b   ND       b
3    c   ND       c
4    d   ND       d
5   ND    e       e
6   ND    f       f
7   ND    g       g
8   ND    h       h

dataset$combine <- dataset[apply(dataset,2, function(x) nchar(x)==1)] #Also works

有时问题是想得够简单...... ;-)

dataset$combine<-c(dataset$col1[1:4], dataset$col2[5:8])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM