如何在R中组合多列
假设我有这个数据集:
age height weight
"1/2/3" "12/15/18" "30/40/37"
我怎样才能拥有像下面的数据集这样的数据集?
age height weight
1 12 30
2 15 40
3 18 37
如果我的数据集是这样的怎么办:
name age height weight
"Jack" "1/2/3" "12/15/18" "30/40/37"
我想要一个如下所示的数据集:
name age height weight
"Jack" 1 12 30
"Jack" 2 15 40
"Jack" 3 18 37
我该怎么做?
Suppose I have this data set:
age height weight
"1/2/3" "12/15/18" "30/40/37"
How can I have a dataset like a dataset below?
age height weight
1 12 30
2 15 40
3 18 37
And what if my dataset was like this:
name age height weight
"Jack" "1/2/3" "12/15/18" "30/40/37"
I want to have a data set like below:
name age height weight
"Jack" 1 12 30
"Jack" 2 15 40
"Jack" 3 18 37
How can I do this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
如果您不介意使用外部软件包,则可以从
TIDYR
软件包中使用axtim_rows()
。If you don't mind using external package, you can use
separate_rows()
from thetidyr
package.如果您的数据采用这样的数据帧格式:
您可以这样做
如果您的数据是命名向量,则相同的代码也将起作用(从问题中不清楚您的数据采用什么格式,但上面的代码在任何情况下都应该起作用只要您的数据名为
df
)由 reprex 包 (v2.0.1)
If your data is in data frame format like this:
You could do
The same code will also work if your data is a named vector (it's not clear from the question what format your data is in, but the above code should work in either case as long as your data is called
df
)Created on 2022-04-10 by the reprex package (v2.0.1)