[英]Error in R of: Can't convert a `tbl_df/tbl/data.frame` object to function
p_values=''
s <- 0
tab <- map_df(list(result),tidy(for(c in colnames(ordered_gene)[5:ncol(ordered_gene)]){
result=t.test(as.numeric(ordered_gene[which(ordered_gene$group=="high"),c]),as.numeric(ordered_gene[which(ordered_gene$group=="low"),c]));
na.omit(result);
p_values <- c(p_values,result$p);
s <- s + 1
}))
The aim of the code above is supposed to print out all the Welch Two Sample t-test results (from the high & low gene groups) and display them as an organized table that lists out the p-value, df, and t value as organized columns of those 2 groups.上面代码的目的是打印出所有 Welch 两个样本 t 检验结果(来自高和低基因组),并将它们显示为一个有组织的表格,其中列出了 p 值、df 和 t 值这两组的有组织的专栏。 But now, it keeps showing the error of: Can't convert a
tbl_df/tbl/data.frame
object to function.但是现在一直显示错误:Can't convert a
tbl_df/tbl/data.frame
object to function。
What do I do?我该怎么办? Thanks!
谢谢!
Two approaches, based on the tidyverse.两种方法,基于 tidyverse。
To begin, generate some data as the OP has not provided any test data.首先,生成一些数据,因为 OP 没有提供任何测试数据。 From the code sample shown, it seems we have a data.frame in wide format, but we don't know how the columns are named.
从显示的代码示例中,我们似乎有一个宽格式的 data.frame,但我们不知道列是如何命名的。
library(tidyverse)
library(broom)
ordered_gene <- tibble(
Group=rep(c("low", "medium", "high"), each=5),
Var1=rnorm(15),
Var2=rnorm(15),
Var3=rnorm(15)
)
> ordered_gene
# A tibble: 15 × 4
Group Var1 Var2 Var3
<chr> <dbl> <dbl> <dbl>
1 low -1.21 -0.212 0.792
2 low 0.674 0.0245 2.05
3 low 0.977 -0.792 -0.519
4 low -1.21 -0.687 0.504
5 low -0.478 -0.864 0.620
6 medium 1.87 -1.44 -1.16
7 medium -1.41 0.325 0.687
8 medium 2.72 0.230 -0.887
9 medium 1.72 0.493 -0.346
10 medium 0.752 -1.86 1.11
11 high -0.560 -1.08 -0.376
12 high -0.819 1.44 -0.906
13 high 0.603 0.0608 -0.704
14 high 0.713 2.59 -0.449
15 high 0.128 1.88 1.13
>
The first approach is very awkward.第一种方法非常尴尬。 The awkwardness is caused by the fact that the input dataset appears not to be tidy.
尴尬是由于输入数据集看起来不整洁造成的。 The second tidies the data and then performs the same analyses, but much more simply.
第二个整理数据,然后执行相同的分析,但更简单。
First approach第一种方法
lapply(
# For each column in ordered_gene, other than Group...
names(ordered_gene %>% select(-Group)),
function(col) {
ordered_gene %>%
# Keep only the data relating to rowes where group is 'low' or 'high'
filter(Group %in% c("low", "high")) %>%
# Apply the following function
group_map(
function(.x, .y) {
# Convert the output of t.test to a data.frame
tidy(
# Perform the t.test using the 'formula' version of the function
t.test(as.formula(paste0(".x$", col, " ~ .x$Group")))
) %>%
# Identify the column being analysed
add_column(Var=col, .before=1)
}
)
}
) %>%
# Bind the list of tibbles returned by lapply into a single tibble
bind_rows()
# A tibble: 3 × 11
Var estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high method alternative
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 Var1 0.263 0.0130 -0.250 0.475 0.649 6.95 -1.05 1.57 Welch Two Sample t-test two.sided
2 Var2 1.48 0.978 -0.506 2.17 0.0870 4.56 -0.323 3.29 Welch Two Sample t-test two.sided
3 Var3 -0.951 -0.260 0.690 -1.74 0.121 7.87 -2.22 0.314 Welch Two Sample t-test two.sided
Second approach第二种方法
# Tidy the data
tidy_gene <- ordered_gene %>%
pivot_longer(
!Group,
names_to="Var",
values_to="Value"
)
tidy_gene
# A tibble: 45 × 3
Group Var Value
<chr> <chr> <dbl>
1 low Var1 -1.21
2 low Var2 -0.212
3 low Var3 0.792
4 low Var1 0.674
5 low Var2 0.0245
6 low Var3 2.05
7 low Var1 0.977
8 low Var2 -0.792
9 low Var3 -0.519
10 low Var1 -1.21
# … with 35 more rows
tidy_gene %>%
# For each variable
group_by(Var) %>%
# Select only rows where Group is 'low' or 'high'
filter(Group %in% c("low", "high")) %>%
# Perform the t-test
group_modify(~tidy(t.test(as.formula(".x$Value ~ .x$Group"))))
# A tibble: 3 × 11
# Groups: Var [3]
Var estimate estimate1 estimate2 statistic p.value parameter conf.low conf.high method alternative
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <chr> <chr>
1 Var1 0.263 0.0130 -0.250 0.475 0.649 6.95 -1.05 1.57 Welch Two Sample t-test two.sided
2 Var2 1.48 0.978 -0.506 2.17 0.0870 4.56 -0.323 3.29 Welch Two Sample t-test two.sided
3 Var3 -0.951 -0.260 0.690 -1.74 0.121 7.87 -2.22 0.314 Welch Two Sample t-test two.sided
The two approaches produce identical results这两种方法产生相同的结果
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.