将strsplit的结果分配给数据框的多列 [英] Assigning results of strsplit to multiple columns of data frame
问题描述
我正在尝试将一个字符向量在一个数据帧内分成三个不同的向量.
I am trying to split a character vector into three different vectors, inside a data frame.
我的数据如下:
> 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个信息(authors
,year
,title
)分成三个不同的列,这样就可以了:
And I would like to split those 3 informations (authors
, year
, title
) into three different columns, so that it would be:
> 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个元素的向量:
I have used strsplit
to split each filename
in a vector of 3 elements:
df$temp <- strsplit(df$filename, " \\(|\\) ")
但是现在,我找不到将每个元素放在单独列中的方法.我可以访问像这样的特定信息:
But now, I can't find a way to put each element in a separate column. I can access a specific information like that:
> df$temp[[2]][1]
[1] "Author2 et al"
但找不到如何将其放在其他列中
but can't find how to put it in the other columns
> df$author <- df$temp[[]][1]
Error
推荐答案
使用tidyr
软件包,这是一个separate
解决方案:
With the tidyr
package, here's a separate
solution:
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
前导空格和尾随空格已被考虑
Leading and trailing spaces have been accounted for
这篇关于将strsplit的结果分配给数据框的多列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!