Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/74.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何对数据框中由行特征选择的多个列和行执行函数?_R - Fatal编程技术网

R 如何对数据框中由行特征选择的多个列和行执行函数?

R 如何对数据框中由行特征选择的多个列和行执行函数?,r,R,我的数据和任务: 每行数据有3列定义特征,2列包含“预测”和“观察”结果。我想对具有相同特征的预测值和观测值进行统计比较 以下是一个示例数据集: metadata <- data.frame("sample" = c(rep("x",8),rep("y",8)), "test" = rep(c("a","b"),8), "strain" = rep(c("i","i","j","j"),4),

我的数据和任务:

每行数据有3列定义特征,2列包含“预测”和“观察”结果。我想对具有相同特征的预测值和观测值进行统计比较

以下是一个示例数据集:

metadata <- data.frame("sample" = c(rep("x",8),rep("y",8)),
                   "test" = rep(c("a","b"),8),
                   "strain" = rep(c("i","i","j","j"),4),
                   "predicted" = sample(1:10,16,replace=T),
                   "observed" = sample(1:10,16,replace=T))
产生:

  sample test strain predicted observed    RMSE
 1      x    a      i         5        2 2.12132
 2      x    a      i         6        6 2.12132
这就是我想要的结果。但我需要为样本、测试和应变的每个独特组合自动完成。(我的数据集远不止此示例)


环顾四周后,我认为dplyr是最好的解决方案。但我愿意接受任何解决方案。提前谢谢你

您可以使用
group\u by

library(dplyr)

metadata %>%
  group_by(sample, test, strain) %>%
  mutate(RMSE = rmse(predicted, observed))

我很高兴它这么简单!非常感谢。
library(dplyr)

metadata %>%
  group_by(sample, test, strain) %>%
  mutate(RMSE = rmse(predicted, observed))