【问题标题】:Assigning results of strsplit to multiple columns of data frame将 strsplit 的结果分配给数据框的多列
【发布时间】:2015-07-11 14:03:38
【问题描述】:

我正在尝试将一个字符向量拆分为三个不同的向量,在一个数据框中。

我的数据是这样的:

> df <- data.frame(filename = c("Author1 (2010) Title of paper", 
                                "Author2 et al (2009) Title of paper",
                                "Author3 & Author4 (2004) Title of paper"),
                   stringsAsFactors = FALSE)

我想将这 3 个信息(authorsyeartitle)分成三个不同的列,这样它会是:

> df
                          filename             author  year   title
 1           Author1 (2010) Title1            Author1  2010  Title1
 2     Author2 et al (2009) Title2      Author2 et al  2009  Title2
 3 Author3 & Author4 (2004) Title3  Author3 & Author4  2004  Title3

我使用strsplit 将每个filename 拆分为3 个元素的向量:

 df$temp <- strsplit(df$filename, " \\(|\\) ")

但现在,我找不到将每个元素放在单独列中的方法。我可以访问这样的特定信息:

> df$temp[[2]][1]
[1] "Author2 et al"

但找不到如何将其放在其他列中

> df$author <- df$temp[[]][1]
Error

【问题讨论】:

    标签: r multiple-columns strsplit


    【解决方案1】:

    你可以从data.table的开发版试试tstrsplit

    library(data.table)#v1.9.5+
     setDT(df)[, c('author', 'year', 'title') :=tstrsplit(filename, ' \\(|\\) ')]
    df
    #                                  filename             author year
    #1:           Author1 (2010) Title of paper           Author1  2010
    #2:     Author2 et al (2009) Title of paper     Author2 et al  2009
    #3: Author3 & Author4 (2004) Title of paper Author3 & Author4  2004
    #             title
    #1:  Title of paper
    #2:  Title of paper
    #3:  Title of paper
    

    编辑:包含 OP 的拆分模式以删除空格。

    【讨论】:

      【解决方案2】:

      使用tidyr 包,这里有一个separate 解决方案:

      separate(df, "filename", c("Author","Year","Title"), sep=" \\(|\\) "), remove=F)
      #                                  filename            Author
      # 1           Author1 (2010) Title of paper           Author1
      # 2     Author2 et al (2009) Title of paper     Author2 et al
      # 3 Author3 & Author4 (2004) Title of paper Author3 & Author4
      #   Year          Title
      # 1 2010 Title of paper
      # 2 2009 Title of paper
      # 3 2004 Title of paper
      

      前导空格和尾随空格都已计算在内

      【讨论】:

        【解决方案3】:
        result <- cbind(df, do.call("rbind", strsplit(df$filename, " \\(|\\) ")))
        colnames(result)[2:4] <- c("author", "year", "title")
        

        【讨论】:

          【解决方案4】:

          数据帧有一个基本的t-方法(转置):

           res <- t( data.frame(  strsplit(df$filename, " \\(|\\) ") ))
           colnames(res) <- c("author", "year", "title")
           rownames(res) <- seq_along(rownames(res) )
           res
          #--------------
            author              year   title           
          1 "Author1"           "2010" "Title of paper"
          2 "Author2 et al"     "2009" "Title of paper"
          3 "Author3 & Author4" "2004" "Title of paper"
          

          【讨论】:

          • 太棒了!最后,我这样做了:df &lt;- cbind(df, t( data.frame( strsplit(df$filename, " \\(|\\) ") ) ) ); colnames(df)[colnames(df) == "1"] &lt;- "author"; colnames(df)[colnames(df) == "2"] &lt;- "year"; colnames(df)[colnames(df) == "3"] &lt;- "title"; rownames(df) &lt;- NULL
          猜你喜欢
          • 1970-01-01
          • 2016-05-21
          • 2020-10-24
          • 2012-06-28
          • 2022-10-12
          • 1970-01-01
          • 1970-01-01
          • 2021-08-22
          相关资源
          最近更新 更多