【问题标题】:How do I combine two columns with offset data?如何将两列与偏移数据结合起来?
【发布时间】:2012-12-10 17:14:56
【问题描述】:

我的数据集包含两列,其中的数据是偏移的 - 类似于:

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

我想将这两个偏移列组合成一个列,其中包含字母 a 到 h,仅此而已。

我的想法是这样的,但是 rbind 不是正确的命令:

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

【问题讨论】:

    标签: r multiple-columns


    【解决方案1】:

    怎么样:

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

    【讨论】:

    • 这是伟大的阿里 - 谢谢。问题是我的实际数据集(这是一个示例)缺少散布在 col1 和 col2 中的数据,所以这段代码也会消除这些行,我不想这样做 - 我想保留所有行。有什么方法可以代替使用行号,就像我在使用 rbind 的示例中尝试做的那样?再次感谢。
    • @Luke,在将数据读入 R 时,您可能需要考虑将 "ND" 转换为实际的 NA 值。
    • 基本上你可以通过逻辑向量的某种组合来实现你想要的。查看help("&amp;")help("|")。并考虑并考虑@AnandaMahto 的建议使用NA,尽管那时你必须使用is.na 而不是==
    【解决方案2】:

    使用sapply 和一个匿名函数:

    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
    

    【讨论】:

      【解决方案3】:

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

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

      【讨论】:

        【解决方案4】:

        另一种方式,使用mapplygsub

         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
        

        根据您对@Andrie 的回答的评论,这也将保留NA 行。

        【讨论】:

          【解决方案5】:

          另一种观点:

          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
          

          【讨论】:

            【解决方案6】:

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

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

            【讨论】:

            • @Morten 他的col1col2 可能是factors...也许你默认设置了options(stringsAsFactors=FALSE)
            猜你喜欢
            • 2015-06-19
            • 2021-11-13
            • 1970-01-01
            • 1970-01-01
            • 2020-10-09
            • 2020-11-02
            • 2021-11-21
            • 2019-11-18
            • 1970-01-01
            相关资源
            最近更新 更多