Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/73.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/magento/5.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在R数据帧中将数据单元转换为长格式的行_R - Fatal编程技术网

在R数据帧中将数据单元转换为长格式的行

在R数据帧中将数据单元转换为长格式的行,r,R,我想重塑我的原始数据,我已经为此使用了一个解决方案,但现在遇到了更复杂的数据集问题 我的数据是这样的 id gender rt 1 2 23, 50, 45, 60, 10 2 1 12, 4, 6 我想将其转换为长格式,但遇到了麻烦,因为在本例中,我有一个单元格rt,我想为每个id设置长格式 以下是我希望它看起来的样子: id gender rt 1 2 23 1 2 50

我想重塑我的原始数据,我已经为此使用了一个解决方案,但现在遇到了更复杂的数据集问题

我的数据是这样的

id    gender    rt
1     2         23, 50, 45, 60, 10
2     1         12, 4, 6
我想将其转换为长格式,但遇到了麻烦,因为在本例中,我有一个单元格rt,我想为每个id设置长格式

以下是我希望它看起来的样子:

id    gender    rt
1     2         23
1     2         50
1     2         45
1     2         60
1     2         10
2     1         12
2     1         4
2     1         6
在这里,与我拥有的其他数据相比,新的复杂性在于rt的长度因id而异

我尝试使用Reforme和ldply,但无法实现我想要的功能,因为我的长数据是一个包含多个观察值的单元格rt,而不是包含单个观察值的多个列


如何将数据转换为所需格式?

使用cSplit,我们可以轻松完成此操作

l<-strsplit(mydf$rt, split = ",")
data.frame(id = rep(mydf$id, sapply(l, length)),gender= rep(mydf$gender, sapply(l, length)), rt = unlist(l))

您不需要外部库或任何东西。使用我所给予的。
library(splitstackshape)
cSplit(df1, 'rt', ', ', 'long')