我有一个如下所示的数据集:
structure(list(A = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("1",
"2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13",
"14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24",
"25"), class = "factor"), T = c(0.04, 0.08, 0.12, 0.16, 0.2,
0.24), X = c(464.4, 464.4, 464.4, 464.4, 464.4, 464.4), Y = c(418.5,
418.5, 418.5, 418.5, 418.5, 418.5), V = c(0, 0, 0, 0, 0, 0),
GD = c(0, 0, 0, 0, 0, 0), ND = c(NA, 0, 0, 0, 0, 0), ND2 = c(NA,
0, 0, 0, 0, 0), TID = structure(c(1L, 1L, 1L, 1L, 1L, 1L), .Label = c("t1",
"t10", "t11", "t12", "t13", "t14", "t15", "t16", "t17", "t18",
"t19", "t2", "t20", "t21", "t22", "t23", "t24", "t25", "t3",
"t4", "t5", "t6", "t7", "t8", "t9"), class = "factor")), .Names = c("A",
"T", "X", "Y", "V", "GD", "ND", "ND2", "TID"), row.names = c(NA,
6L), class = "data.frame")
Run Code Online (Sandbox Code Playgroud)
我想为每个TID选择所有变量的前80个观测值.到目前为止,我只能使用代码使用第一个TID执行此操作:
sub.data1<-NM[1:80, ]
Run Code Online (Sandbox Code Playgroud)
我怎么能为我所有其他TID做到这一点?
谢谢!
我会做:
lapply(split(dat, dat$TID), head, 80)
Run Code Online (Sandbox Code Playgroud)
它返回一个包含80(或更少)行的data.frames列表.相反,如果您想将所有内容都放在一个data.frame中:
do.call(rbind, lapply(split(dat, dat$TID), head, 80))
Run Code Online (Sandbox Code Playgroud)
使用功能ddply()的plyr,你可以通过TID拆分数据,然后选择福斯特80 head(),然后再次把所有在一个数据帧,
library(plyr)
ddply(NM, .(TID), head, n = 80)
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1069 次 |
| 最近记录: |