按组返回 NA 的函数
Function by group returning NAs
我有以下数据框,我正在尝试为其创建函数:
df<- structure(list(BLG = c(37.037037037037, 12.0603015075377, 93.5593220338983,
3.96563119629874, 77.634011090573, 71.608040201005, 3.96563119629874,
119.775421085465, 44.8765893792072), GSF = c(0, 0, 0, 0, 11.090573012939,
0, 0, 0, 0), LMB = c(66.6666666666667, 24.1206030150754, 40.6779661016949,
31.7250495703899, 73.9371534195933, 67.8391959798995, 31.7250495703899,
22.4578914535246, 31.413612565445), YLB = c(0, 0, 0, 0, 14.7874306839187,
0, 0, 0, 0), BLC = c(3.7037037037037, 0, 4.06779661016949, 7.93126239259749,
7.39371534195933, 11.3065326633166, 7.93126239259749, 3.74298190892077,
22.4382946896036), WHC = c(7.40740740740741, 0, 0, 0, 0, 0, 0,
7.48596381784155, 4.48765893792072), RSF = c(0, 0, 0, 0, 0, 0,
0, 0, 4.48765893792072), CCF = c(3.7037037037037, 0, 8.13559322033898,
0, 0, 0, 0, 0, 0), BLB = c(0, 0, 0, 0, 0, 0, 0, 0, 0), group = c(1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L)), row.names = c(NA, -9L), class = c("data.table",
"data.frame"))
函数
p_true<- c(83, 10, 47, 8, 9, 6, 12, 5, 8) #true value for each column
estimate2 = function(df) {
y_est2 = df
sqrt(mean((y_est2-p_true)^2))/p_true*100
}
final<- df %>%
group_by(group) %>%
group_modify(~ as.data.frame.list(estimate2(.)))
最终输出应该是一个 3x9 数据框:每组每列一个值。可以用 plyr::ddply(df, .(group), estimate2)
得到预期的输出格式
即使不尝试 运行 使用 estimate2(df)
跨组的函数(并删除组列),它仍然说参数不是逻辑的或数字的;返回 NA.
我不确定为什么,因为我的 运行 函数与这个非常相似,只是内部的实际方程略有不同,而且它们工作正常。
有人知道我哪里错了吗?
问题是 mean
命令。使用 ?mean
查看它的帮助,它说:
x
An R object. Currently there are methods for numeric/logical vectors and date, date-time and time interval objects. Complex vectors are allowed for trim = 0, only.
但是您想计算数据框三行的平均值。
我不完全确定以下是否是您想要的,但您可以取消列出数据框,使其成为矢量。除以 p_true
然后再循环到这个向量的长度。然后您可以将结果再次合并到一个数据框中:
p_true<- c(83, 10, 47, 8, 9, 6, 12, 5, 8) #true value for each column
estimate2 = function(df) {
y_est2 = df
return_df <- as.data.frame(t(sqrt(mean(unlist((y_est2-p_true)^2)))/p_true*100))
names(return_df) <- names(y_est2)
return(return_df)
}
final<- df %>%
group_by(group) %>%
group_modify(~ as.data.frame.list(estimate2(.)))
这个returns:
# A tibble: 3 x 10
# Groups: group [3]
group BLG GSF LMB YLB BLC WHC RSF CCF BLB
<int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1 38.7 321. 68.3 401. 357. 535. 268. 642. 401.
2 2 45.9 381. 81.1 477. 424. 635. 318. 763. 477.
3 3 45.6 378. 80.4 473. 420. 630. 315. 756. 473.
我有以下数据框,我正在尝试为其创建函数:
df<- structure(list(BLG = c(37.037037037037, 12.0603015075377, 93.5593220338983,
3.96563119629874, 77.634011090573, 71.608040201005, 3.96563119629874,
119.775421085465, 44.8765893792072), GSF = c(0, 0, 0, 0, 11.090573012939,
0, 0, 0, 0), LMB = c(66.6666666666667, 24.1206030150754, 40.6779661016949,
31.7250495703899, 73.9371534195933, 67.8391959798995, 31.7250495703899,
22.4578914535246, 31.413612565445), YLB = c(0, 0, 0, 0, 14.7874306839187,
0, 0, 0, 0), BLC = c(3.7037037037037, 0, 4.06779661016949, 7.93126239259749,
7.39371534195933, 11.3065326633166, 7.93126239259749, 3.74298190892077,
22.4382946896036), WHC = c(7.40740740740741, 0, 0, 0, 0, 0, 0,
7.48596381784155, 4.48765893792072), RSF = c(0, 0, 0, 0, 0, 0,
0, 0, 4.48765893792072), CCF = c(3.7037037037037, 0, 8.13559322033898,
0, 0, 0, 0, 0, 0), BLB = c(0, 0, 0, 0, 0, 0, 0, 0, 0), group = c(1L,
1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L)), row.names = c(NA, -9L), class = c("data.table",
"data.frame"))
函数
p_true<- c(83, 10, 47, 8, 9, 6, 12, 5, 8) #true value for each column
estimate2 = function(df) {
y_est2 = df
sqrt(mean((y_est2-p_true)^2))/p_true*100
}
final<- df %>%
group_by(group) %>%
group_modify(~ as.data.frame.list(estimate2(.)))
最终输出应该是一个 3x9 数据框:每组每列一个值。可以用 plyr::ddply(df, .(group), estimate2)
即使不尝试 运行 使用 estimate2(df)
跨组的函数(并删除组列),它仍然说参数不是逻辑的或数字的;返回 NA.
我不确定为什么,因为我的 运行 函数与这个非常相似,只是内部的实际方程略有不同,而且它们工作正常。
有人知道我哪里错了吗?
问题是 mean
命令。使用 ?mean
查看它的帮助,它说:
x
An R object. Currently there are methods for numeric/logical vectors and date, date-time and time interval objects. Complex vectors are allowed for trim = 0, only.
但是您想计算数据框三行的平均值。
我不完全确定以下是否是您想要的,但您可以取消列出数据框,使其成为矢量。除以 p_true
然后再循环到这个向量的长度。然后您可以将结果再次合并到一个数据框中:
p_true<- c(83, 10, 47, 8, 9, 6, 12, 5, 8) #true value for each column
estimate2 = function(df) {
y_est2 = df
return_df <- as.data.frame(t(sqrt(mean(unlist((y_est2-p_true)^2)))/p_true*100))
names(return_df) <- names(y_est2)
return(return_df)
}
final<- df %>%
group_by(group) %>%
group_modify(~ as.data.frame.list(estimate2(.)))
这个returns:
# A tibble: 3 x 10
# Groups: group [3]
group BLG GSF LMB YLB BLC WHC RSF CCF BLB
<int> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 1 38.7 321. 68.3 401. 357. 535. 268. 642. 401.
2 2 45.9 381. 81.1 477. 424. 635. 318. 763. 477.
3 3 45.6 378. 80.4 473. 420. 630. 315. 756. 473.