【问题标题】:R: Write for loop to calculate difference and store in new variableR:编写for循环以计算差异并存储在新变量中
【发布时间】:2017-08-28 12:03:05
【问题描述】:

我查看了有关此主题的不同问题,但到目前为止,没有一个问题能帮助我获得我想要的东西。

我有一个数据框,有两个变量(standardized_'testname')和'predicted_standardized_'testname')。现在,我想计算两者之间的差异并将其存储在一个名为“testname”_finalscore 的新变量中。

由于我有大约 19 种不同的测试,我想在 R 中使用 for 循环来执行此操作 - 但我是编写这类循环的新手,我被卡住了。

我有一个测试列表,其中包含单独测试的所有名称:

testlist <- c("vlgt_ltfr", "vlgt_recog", 
          "vlgt_imrec", "wms_imrec", 
          "wms_delrec", "fluency_dier", 
          "fluency_beroep", "tapdom",
          "tapndom", "traila", "trailb",
          "erik_congruent", "erik_percincong",
          "erik_incongruent", "stroop_baseline",
          "stroop_interference", "subrs", "tmt_interference")

为此,我编写了一个循环来计算标准化和预测标准化的分数。

例子:

for( test in testlist){
      patdat[,paste0('standardized_',test)] <- (patdat[,test] - tempmean) / tempsd
      patdat[,paste0('predicted_standardized_',test)] <- coef(mymod)[1] + coef(mymod)[2]*patdat[,'p_age'] + coef(mymod)[3]*patdat[,'nlviq']

}

在此之后,我创建了不同的循环(不起作用),在其中我尝试计算差异并将其存储在一个新变量中:

for( test in testlist){
  normdata[,paste0(test,'_finalscore')] <- (normdata[,paste0('standardized_', test)] - normdata[,paste0('predicted_standardized_', test)])
 }

for(test in testlist){
  normdata[,paste0(test, '_finalscore')] <- normdata[get('standardized_',test)] - normdata[get('predicted_standardized_'), test]
}

for(test in testlist){
  normdata[,paste0(test, '_finalscore')] <- (normdata['standardized_',test] - normdata['predicted_standardized_', test])
}

我确实得到了一个带有 'testname'_finalscore 的变量,但是它是空的。我认为我的索引错误,并且可能有一个函数可以用来解决这个问题 - 但我还没有找到它。

数据示例

> normdata$standardized_subrs
 [1] -0.45551  0.61058  0.18414  0.18414 -0.13568 -1.30838  0.39736
 [8]  0.71719 -0.13568 -0.13568  0.29075  0.18414  1.99649 -1.62821

> normdata$predicted_standardized_subrs
 [1] -0.458274  0.174143 -0.492066 -0.414063  0.081612  0.488208
 [7]  0.399994  0.416249 -0.113008 -0.398671  0.943571  0.316543

我想要的是一个看起来像这样的变量“subrs_finalscore”,但是对于testlist中的所有测试:

> normdata$standardized_subrs - normdata$predicted_standardized_subrs
 [1]  0.002764  0.436435  0.676208  0.598205 -0.217296 -1.796589
 [7] -0.002633  0.300938 -0.022676  0.262987 -0.652819 -0.132400

提前致谢。

【问题讨论】:

  • normdata &lt;- data.frame( standardized_subrs = c(-0.45551, 0.61058, 0.18414), predicted_standardized_subrs = c(-0.458274, 0.174143, -0.492066) ) ; testlist &lt;- c("subrs") ; for( test in testlist){ normdata[,paste0(test,'_finalscore')] &lt;- (normdata[,paste0('standardized_', test)] - normdata[,paste0('predicted_standardized_', test)]) } 在我身边工作。它在你的身上吗?如果不是,是什么问题(错误)?
  • 这里也可以!不知道为什么以前没有,可能是错字或其他什么...谢谢!我无法理解出了什么问题。
  • 在您的示例中,standardized_subrs predicted_standardized_subrs 的观察量不同

标签: r variables for-loop


【解决方案1】:

对于每个测试,我们的数据框中都有 standardizedpredicted_standardized 列。这是一个很难得到问题答案的表格。

我们想要计算两个数字之间的差并将其存储。如果数据如下所示:

TestName Standardized Predicted
subrs        -0.45551 -0.458274
subrs         0.61058  0.174143
subrs         0.18414 -0.492066
...

我们不是以包含许多列的宽格式存储数据(每个测试两列),而是以只有三列的长格式存储数据:测试名称、标准化值和预测值。这称为tidying 数据,或将其置于tidy 格式。

如果我们在名为tidy_data 的框架中有tidy 格式的数据,那么计算差异就像...

library(tidyverse)
tidy_data %>% mutate(FinalScore = Predicted - Standardized)

mutate 将新列添加到具有计算值的框架中。

那么我们如何以tidy 的形式获取它?这有点工作,但如果我们把原来的宽数据框改成这样......

tidy_data = data %>%
  mutate(row_num = row_number()) %>%
  gather(key, value, -row_num) %>%
  mutate(IsPredicted = ifelse(grepl("predicted", key), "Predicted", "Standardized"),
         TestName = gsub("predicted_standardized_|standardized_", "", key)) %>%
  select(TestName, IsPredicted, value, row_num) %>%
  spread(IsPredicted, value) %>%
  select(-row_num)

我们得到了我们想要的整洁的表格。

【讨论】:

  • 我记得在某处读到过关于第三范式及其好处的文章...... :)
猜你喜欢
  • 2021-01-05
  • 1970-01-01
  • 1970-01-01
  • 2021-11-19
  • 2019-06-17
  • 1970-01-01
  • 2022-08-11
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多