使用另一个矩阵中的值替换嵌套列表中的值

时间:2016-05-28 21:32:18

标签: r

我有一个包含以下28个元素的列表,这两个元素是两个2个元素的组合(SG1,SG2,... SG8):

str(combinations)
List of 28
 $ : chr [1:2] "SG1" "SG2"
 $ : chr [1:2] "SG1" "SG3"
 $ : chr [1:2] "SG1" "SG4"
...

我已将t.test的结果存储在对象中:

results <- lapply(seq_along(combinations), function (n) {
  mydatatemp <- mydata[with(mydata, Subgroup %in% unlist(combinations2[n]) & Group %in% c("G1", "G3")),]
  result <- t.test(mydatatemp[
                   mydatatemp$Subgroup == sapply(combinations[n], "[",1),4],
                   mydatatemp[mydatatemp$Subgroup == sapply(combinations[n], "[", 2),4],
                   alternative="two.sided", var.equal=TRUE)
             return(result)})

和我的str(result)是这样的,当然有28个列表元素像这样一个接一个:

List of 28
 $ :List of 9
  ..$ statistic  : Named num -6.9
  .. ..- attr(*, "names")= chr "t"
  ..$ parameter  : Named num 38
  .. ..- attr(*, "names")= chr "df"
  ..$ p.value    : num 3.33e-08
  ..$ conf.int   : atomic [1:2] -0.301 -0.164
  .. ..- attr(*, "conf.level")= num 0.95
  ..$ estimate   : Named num [1:2] 0.196 0.429
  .. ..- attr(*, "names")= chr [1:2] "mean of x" "mean of y"
  ..$ null.value : Named num 0
  .. ..- attr(*, "names")= chr "difference in means"
  ..$ alternative: chr "two.sided"
  ..$ method     : chr " Two Sample t-test"
  ..$ data.name  : chr [1:2] "mydatatemp[mydatatemp$Subgroup == sapply(combinations[n], \"[\", 1),  and mydatatemp[mydatatemp$Subgroup == sapply(combinations[n], \"[\""| __truncated__ "    4] and     4]"
  ..- attr(*, "class")= chr "htest"

如何使用以下$data.name代码重命名所有chr [1:2]元素paste

paste(matrix(unlist(combinations), ncol = 2, byrow = TRUE)[,1], matrix(unlist(combinations), ncol = 2, byrow = TRUE)[,2], sep = " vs. ")

我这样做的原因是在$ data.name元素中替换那些难看的字符。

0 个答案:

没有答案