我有一组包含26列的数据。我想对以 id 和类型开头的所有列进行逐行平均:
dput(head(df,4))
structure(list(P7 = c(0.266687798848186, 0.243782451327742, 0.256932437720159,
0.298861598151174), P4 = c(0.198233672667731, 0.0917952258522064,
0.0911852809187542, 0.0896079263551856), Cz = c(0.236398290801764,
0.0864552727199747, 0.0745517747485495, 0.141094205953345), Pz = c(0.134887167694073,
0.0747942533151883, 0.0955856952160322, 0.0351423350784724),
P3 = c(0.191077740741983, 0.0671666001582164, 0.15631145309625,
0.0784718768200876), P8 = c(0.746777191136397, 0.560405877368518,
0.600967378548292, 0.536464030748274), O1 = c(0.178986458890412,
0.0630053138978704, 0.0774329882095566, 0.0691776190155007
), O2 = c(0.366020592243935, 0.145930065883011, 0.265438304721104,
0.161825120181983), T8 = c(0.166974498667532, 0.193021656050489,
0.213308929827665, 0.213890447571556), F8 = c(0.263718560854391,
0.131907696650378, 0.111178337073373, 0.231875668810878),
C4 = c(0.269114234615308, 0.125279019411176, 0.0913937842967784,
0.230858661872378), F4 = c(0.231465667185867, 0.161178083318531,
0.216487717434052, 0.171471377435883), FP2 = c(0.180752958268289,
0.226079727116054, 0.284591837287001, 0.0666576126271971),
Fz = c(0.25772461457411, 0.11393640031093, 0.0915436977625258,
0.22075971063348), C3 = c(0.251047419087495, 0.116387810932296,
0.0658483596391047, 0.217611910888088), F3 = c(0.265273001053431,
0.126774002131214, 0.125733221613179, 0.233189075725804),
FP1 = c(0.142119646773935, 0.221130756519951, 0.318851412027302,
0.0888396971594962), T7 = c(0.249976523730528, 0.0650165785671064,
0.108231023089731, 0.235791252265216), F7 = c(0.244199373827059,
0.116417055069381, 0.0661299818628654, 0.237627682382219),
id = c(1L, 1L, 1L, 1L), Time = 1:4, Segment = c(1L, 1L, 1L,
1L), TimeLower = c(-Inf, -Inf, -Inf, -Inf), TimeUpper = c(180,
180, 180, 180), Group = structure(c(2L, 2L, 2L, 2L), .Label = c("C",
"N"), class = "factor"), type = structure(c(1L, 1L, 1L, 1L
), .Label = c("alpha", "beta", "gamma", "delta", "theta"), class = c("ordered",
"factor"))), .Names = c("P7", "P4", "Cz", "Pz", "P3", "P8",
"O1", "O2", "T8", "F8", "C4", "F4", "FP2", "Fz", "C3", "F3",
"FP1", "T7", "F7", "id", "Time", "Segment", "TimeLower", "TimeUpper",
"Group", "type"), row.names = c(NA, 4L), class = "data.frame")
例如,我希望以行方式平均属于列Cz,C4和C3的所有数据类型= alpha,beta,gamma,delta和theta。并通过列ID进行此操作。
答案 0 :(得分:1)
我们可以使用tidyverse
library(dplyr)
df %>%
group_by(id, type) %>%
summarise_at(vars(starts_with("C")), mean) %>%
transmute(avg = mean(Cz, C4, C3))