Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/64.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,我有以下数据。帧dat structure(list(tlv = c(1654, 1234, 1832, 1118, 987, 8887, 6089.66666666666, 7081.38095238095, 8073.09523809523, 9064.80952380951), form1 = c(8887, 456, 890, 456, 544, 632, 720, 808, 896, 984), form2 = c(1, 2, 3, 4, 5, 6, 7, 8, 9, 10),

我有以下数据。帧dat

structure(list(tlv = c(1654, 1234, 1832, 1118, 987, 8887, 6089.66666666666, 
7081.38095238095, 8073.09523809523, 9064.80952380951), form1 = c(8887, 
456, 890, 456, 544, 632, 720, 808, 896, 984), form2 = c(1, 2, 
3, 4, 5, 6, 7, 8, 9, 10), form3 = c(1118, 987, 1654, 1234, 1832, 
1118, 987, 1654, 1234, 1832)), row.names = c(NA, -10L), class = c("tbl_df", 
"tbl", "data.frame"))
我编写了以下代码,以便在以“form”和tlv开头的所有变量之间执行ttest。我的目标是保存以下结果

比较统计值

k=which(grepl("^form",colnames(dat)))
all_t_test = lapply(dat[,k], FUN=function(x) {
            result = t.test(x, dat$tlv)
            data.frame(
              'comparison'=paste(x,collapse="-"),
              'statistic'=result$statistic,
              'p-value'=result$p.value
            )

})
all_t_test =  do.call(rbind,all_t_test)
library(readxl)
library(writexl)
write_xlsx(all_t_test,"ttest_results.xlsx")
从统计上看,它是有效的。但在“比较”一栏的结果中,它给出了以下值

NA-NA-NA-NA-NA-NA-NA-NA-NA-NA
tlv-form1-form2-form3-NA-NA-NA-NA-NA-NA
NA-NA-NA-NA-NA-NA-NA-NA-NA-NA
而不是

form1
form2
form2

我错在哪里?

这应该可以解决您的问题:

k=which(grepl("^form",colnames(dat)))

ttest_results <- function(x, data){
  result = t.test(data[,x], data$tlv)
  data.frame(
    'comparison'=paste(colnames(data)[x]),
    'statistic'=result$statistic,
    'p-value'=result$p.value
  )
}

all_t_test = lapply(k, FUN=ttest_results, data=dat )
all_t_test =  do.call(rbind,all_t_test)

library(readxl)
library(writexl)

write_xlsx(all_t_test,"ttest_results.xlsx")
k=which(grepl(“^form”,colnames(dat)))

t测试结果正如@42-正确指出的那样,
lapply
call
x
中的值是列中的值,而不是列名,因此当您执行
paste(x,collapse=“-”
时,它会粘贴值而不是列名。试试这个版本-

k = grep("^form",colnames(dat)) #No need to use which with grepl, use grep

ttest_results <- lapply(k, FUN=function(x) {
   result = t.test(dat[[x]], dat$tlv)
   data.frame(
     comparison = names(dat)[x],
     statistic = result$statistic,
     p_value = result$p.value
    )
})

ttest_results
#[[1]]
#  comparison statistic p_value
#t      form1     -2.22  0.0404

#[[2]]
#  comparison statistic p_value
#t      form2     -4.13 0.00257

#[[3]]
#  comparison statistic p_value
#t      form3     -2.89  0.0175

do.call(rbind, ttest_results)
#   comparison statistic p_value
#t       form1     -2.22 0.04042
#t1      form2     -4.13 0.00257
#t2      form3     -2.89 0.01745
k=grep(“^form”,colnames(dat))#无需将其与grepl一起使用,请使用grep

t测试结果错误在
comparison=paste(x,collapse=“-”)
HI@42-我能理解错误吗?为什么我得不到列名?因为你传递了值。列名将作为
names(.)
colnames(.)
@42传递-我正在尝试此操作,但我得到错误:“comparison”=paste(colnames(dat[x],do.NULL=TRUE,prefix=“col”),collapse=“-”),您无法将所有列名粘贴在一起,这是您的代码几乎要做的。您需要将列名从第一个
lappy
参数表达式分别传递给函数,或者使用数字索引引用它们。顺便说一句,您还缺少
colnames(dat[x])
的右括号。请注意@RonakShah和lunar_props如何传递列索引,以允许引用单个列值和单个列名。HI@lunar_props。我得到以下错误:FUN中的错误(X[[I]],…):当我运行行all\u t\u test=lappy(k,FUN=ttest\u results,data=dat)时未使用的参数(data=dat)