R 将数据帧中的向量拆分为2个向量

R 将数据帧中的向量拆分为2个向量,r,R,我需要将数据帧中的一个向量(last.first)拆分为两个单独的向量(firstname,lastname),然后将这两个向量放回数据帧中。我该怎么办。您可以使用strsplit拆分名称,使用任何分隔第一个和第二个名称的方法,而不是使用”(在我的示例中为空格) 这会给你一个清单。可以通过ldply或unlist到matrix person.names <- c("Adam Smith", "Max Webber") temp.list <- strsplit(person.name

我需要将数据帧中的一个向量(last.first)拆分为两个单独的向量(firstname,lastname),然后将这两个向量放回数据帧中。我该怎么办。

您可以使用
strsplit
拆分名称,使用任何分隔第一个和第二个名称的方法,而不是使用
(在我的示例中为空格)

这会给你一个清单。可以通过
ldply
unlist
matrix

person.names <- c("Adam Smith", "Max Webber")
temp.list <- strsplit(person.names, " ")
names.df <- ldply(temp.list, function (x) data.frame(first = x[1], second = x[2]))


  first second
1  Adam  Smith
2   Max Webber

查看我的“splitstackshape”软件包中的
cSplit
函数。查看
separate
from
tidyr
。查看
matrix(unlist(temp.list), ncol = 2, byrow = TRUE)