r - 将每 5 个观察值发送到 r 中的下一行
问题描述
我是 R 新手,我的数据集有问题。我创建了一个数据框,我的所有数据都在 1 行中,我需要将每 5 个观察值发送到下一行:
My current dataset called ndf:
Col_1 Col_2 Col_3 Col_4 ... Col_4005
1 text1 text2 text3 text4 text4005
What I need:
Col_1 Col_2 Col_3 Col_4 Col_5
1 text1 text2 text3 text4 text5
2 text6 text7 text8 text9 text10
...
801 text4001 text4002 text4003 text4004 text4005
我怎样才能做到这一点?
我尝试使用循环,但它不起作用。我还使用了这个不起作用的功能:
ndf<-ndf[rep(seq_len(ncol(ndf)), each = 5), ]
解决方案
您可以使用matrix
:
matrix(ndf, ncol=5, byrow=TRUE)
# [,1] [,2] [,3] [,4] [,5]
#[1,] "text1" "text2" "text3" "text4" "text5"
#[2,] "text6" "text7" "text8" "text9" "text10"
#[3,] "text11" "text12" "text13" "text14" "text15"
#[4,] "text16" "text17" "text18" "text19" "text20"
数据:
ndf <- data.frame(t(paste0("text", 1:20)))
推荐阅读
- javascript - 转换为 UTC 在 Javascript 中给出 NaN 错误
- javascript - 没有重载匹配此调用。最后一个重载给出了以下错误。类型 '"text"' 不能分配给类型 '"json"'
- reactjs - 用钩子动画出 React 组件 (componentWillUnmount)
- android - 无法将自 Fragment 以来的参数传递给子 Fragment
- python - 与 Python 中的 string.strip([char]) 函数有点混淆
- r - 是否可以在 R 脚本中进行只读注释?
- ios - 用于 PWA 中推送通知的 iOS 原生应用程序包装器
- c# - Xamarin vs 2019 multiproject await TranslateTo(100,100,2000) 延迟不起作用?
- spring - httpclient发布基本身份验证
- amazon-web-services - AWS QuickSight 中地理地图可视化的最大数据点限制