使用字符串绑定对象并使用 R 中对象的名称获取列名称

binding objects with a string and getting the column names with the name of the object in R

我有多个具有共同字符串的对象

resultsPetal.Length
resultsPetal.Width
resultsSetal.Length
resultsSetal.Width

当我对它们使用 cbind 时,我得到了这个列表

          resultSepal.Length                                        
statistic 138.9083                                                  
parameter Numeric,2                                                 
p.value   1.505059e-28                                              
method    "One-way analysis of means (not assuming equal variances)"
data.name "dat[, i] and dat$Species"                                
          resultSepal.Width                                         
statistic 45.01204                                                  
parameter Numeric,2                                                 
p.value   1.432735e-14                                              
method    "One-way analysis of means (not assuming equal variances)"
data.name "dat[, i] and dat$Species"   

但是,我有多个对象并且想加入它们而不必为每个对象写一行,所以我使用了

test=do.call(cbind, lapply(ls(pattern ="result"), get))

这让我感动

          [,1]                                                      
statistic 1828.092                                                  
parameter Numeric,2                                                 
p.value   2.693327e-66                                              
method    "One-way analysis of means (not assuming equal variances)"
data.name "dat[, i] and dat$Species"                                
          [,2]                                                      
statistic 1276.885                                                  
parameter Numeric,2                                                 
p.value   4.138739e-64                                              
method    "One-way analysis of means (not assuming equal variances)"
data.name "dat[, i] and dat$Species"                                
          [,3]                                                      
statistic 138.9083                                                  
parameter Numeric,2                                                 
p.value   1.505059e-28                                              
method    "One-way analysis of means (not assuming equal variances)"
data.name "dat[, i] and dat$Species"                                
          [,4]                                                      
statistic 45.01204                                                  
parameter Numeric,2                                                 
p.value   1.432735e-14                                              
method    "One-way analysis of means (not assuming equal variances)"
data.name "dat[, i] and dat$Species"

                         

如您所见,我丢失了对象名称,而且我完全不知道如何解决这个问题。我能做什么?

这里是再现性的完整代码

dat <- iris# remove one level to have only two groups
#dat <- subset(dat, Species != "setosa")
dat$Species <- factor(dat$Species)# boxplots and t-tests for the 4 variables at once
for (i in 1:4) { # variables to compare are variables 1 to 4
  boxplot(dat[, i] ~ dat$Species, # draw boxplots by group
          ylab = names(dat[i]), # rename y-axis with variable's name
          xlab = "Species"
  )
  print(oneway.test(dat[, i] ~ dat$Species)) # print results of t-test
  assign(paste0("result", names(dat[i])), (oneway.test(dat[, i] ~ dat$Species)))
}


test=cbind(resultSepal.Length, resultSepal.Width)
test=do.call(cbind, lapply(ls(pattern ="result"), get))

您可以使用的方法很少:

使用mget:

do.call(cbind, mget(ls(pattern = 'result')))

使用sapply:

sapply(ls(pattern ="result"), get)

为列表命名:

l <- ls(pattern ="result")
do.call(cbind, lapply(setNames(l,l), get))