替换有x#行,数据有x#aggregate R.

时间:2015-09-18 18:35:50

标签: r aggregate

我有一个data.frames列表,mrns[[i]],并希望为每个mrns[[i]]$avg.hr.prhr创建一个新变量,即每小时的平均心率。

我的代码和错误:

for (i in 1:310) {
  mrns[[i]]$avg.hr.prhr <- aggregate(raw.Hour ~ raw.HR, data=mrns[[i]], mean)
}

Error in `$<-.data.frame`(`*tmp*`, "avg.hr.prhr", value = list(raw.HR = c(46L,  : 
  replacement has 32 rows, data has 93

我也尝试使用data.table并且遇到了同样的错误,我还在运行循环之前创建了一个空变量:

for (i in 1:310) {
  mrns[[i]]$avg.hr.prhr <- ""
}

我还检查了几个data.frames中每个变量的行,它们似乎是相同的行数。

length(mrns[[1]]$raw.HR)
[1] 93
length(mrns[[1]]$raw.Hour)
[1] 93

有人有任何建议吗?

修改

尝试使用av​​e而不是聚合:

for (i in 1:310) {
  mrns[[i]]$avg.hr.prhr <- ave(raw.HR ~ raw.Hour , mrns[[i]], FUN=mean)
}

Error in rep(value, length.out = nrows) : 
  attempt to replicate an object of type 'language'
In addition: Warning messages:
1: In split.default(x, g) :
  data length is not a multiple of split variable
2: In split.default(seq_along(x), f, drop = drop, ...) :
  data length is not a multiple of split variable

for (i in 1:310) {
  mrns[[i]]$avg.hr.prhr <- ave(raw.HR, raw.Hour, mrns[[i]])
}

Error in interaction(...) : object 'raw.Hour' not found

与之相关的是我知道raw.Hour不是一个对象,它只是一个变量名称

names(mrns[[i]])
 [1] "raw.Number"         "raw.Reading_Status" "raw.Month"          "raw.Day"           
 [5] "raw.Year"           "raw.Hour"           "raw.Minute"         "raw.Systolic"      
 [9] "raw.Diastolic"      "raw.MAP"            "raw.PP"             "raw.HR"            
[13] "raw.Event_Code"     "raw.Edit_Status"    "raw.Diary_Activity" "na.strings"        
[17] "raw.facility"       "raw.lastname"       "raw.firstname"      "raw.id"            
[21] "raw.hookup"         "raw.datetime"       "raw.mrn"            "unis"              
[25] "ar.value"           "ar.cat"             "baseline.visit"     "visit.date.1"      
[29] "total.sleep.time"   "ID"                   

2 个答案:

答案 0 :(得分:2)

for (i in 1:310) {
  mrns[[i]]$avg.hr.prhr <- with(mrns[[i]], ave(raw.HR, raw.Hour))
}

感谢@PierreLafortune。

答案 1 :(得分:0)

lapplydplyr呢?

    library(dplyr)
    new_list <- lapply(mrns, function(i) { 
         i %>% group_by(raw.Hour) %>% 
         mutate(avg.hr.prhr = mean(raw.HR)) %>% 
         ungroup()
   })