如何将summarize()函数的结果放入r中的dataframe中
我做了一个最小的可重现示例,但是我的真实数据确实很大
a_p_ <-c(0.1, 0.3, 0.03, 0.03)
b_p_ <-c(0.2, 0.003, 0.1, 0.00001)
c_2<-c(1,2,5,23)
c_p_<-c(0.001, 0.002,0.002,0.00001)
results_1<-data.frame(a_p_,b_p_,c_2,c_p_)
a_p_ <-c(0.3, 0.02, 0.43, 0.44)
b_p_ <-c(0.00002, 0.3, 0.8, 0.005)
c_2 <-c(88,4,55,88)
c_p_<-c(0.1, 0.002,0.002,0.1)
results_2<-data.frame(a_p_,b_p_,c_2,c_p_)
,所以我有两个数据集。一个是“结果_1”,另一个是“结果_2”,
然后,我想创建新的数据框架(数据框架名称为type1error) 其中包含以下示例。
更具体地说,我希望这是我的新DataFrame(type1error)的第一行
> results_1 %>%
+ summarise(across(contains("_p_"), ~ mean(.x > 0.05)))
a_p_ b_p_ c_p_
1 0.5 0.5 0
,这是我的数据框架的第二行(type 1错误),
> results_2 %>%
+ summarise(across(contains("_p_"), ~ mean(.x > 0.05)))
a_p_ b_p_ c_p_
1 0.75 0.5 0.5
所以我做的是..
# make empty holder
type1error<-as.data.frame(matrix(nrow = 2))
for(i in 1:2){
# read the data
if(i==1){
results<-results_1
}
if(i==2){
results<-results_2
}
# mean() You can use mean() to get the proportion of TRUE of a logical vector.
type1error[i,]<-results %>%
summarise(across(contains("_p_"), ~ mean(.x > 0.05)))
type1error$conditions[i] <- i
}
但是我收到了这样的警告消息,结果似乎不是我所期望的 (总结每一行的结果)
Warning messages:
1: In `[<-.data.frame`(`*tmp*`, i, , value = list(a_p_ = 0.5, b_p_ = 0.5, :
provided 3 variables to replace 2 variables
2: In `[<-.data.frame`(`*tmp*`, i, , value = list(a_p_ = 0.75, b_p_ = 0.5, :
provided 3 variables to replace 2 variables
如何解决此问题?
I made a minimal reproducible example, but my real data is really huge
a_p_ <-c(0.1, 0.3, 0.03, 0.03)
b_p_ <-c(0.2, 0.003, 0.1, 0.00001)
c_2<-c(1,2,5,23)
c_p_<-c(0.001, 0.002,0.002,0.00001)
results_1<-data.frame(a_p_,b_p_,c_2,c_p_)
a_p_ <-c(0.3, 0.02, 0.43, 0.44)
b_p_ <-c(0.00002, 0.3, 0.8, 0.005)
c_2 <-c(88,4,55,88)
c_p_<-c(0.1, 0.002,0.002,0.1)
results_2<-data.frame(a_p_,b_p_,c_2,c_p_)
so, I have two dataset. the one is "results_1" and the other is "results_2"
and then, I want to create new dataframe (data frame name is type1error)
that contains the following examples.
More specific, I want this to be the first row of my new dataframe (type1error)
> results_1 %>%
+ summarise(across(contains("_p_"), ~ mean(.x > 0.05)))
a_p_ b_p_ c_p_
1 0.5 0.5 0
and this to be my second row of my dataframe (type 1 error)
> results_2 %>%
+ summarise(across(contains("_p_"), ~ mean(.x > 0.05)))
a_p_ b_p_ c_p_
1 0.75 0.5 0.5
so what I did is..
# make empty holder
type1error<-as.data.frame(matrix(nrow = 2))
for(i in 1:2){
# read the data
if(i==1){
results<-results_1
}
if(i==2){
results<-results_2
}
# mean() You can use mean() to get the proportion of TRUE of a logical vector.
type1error[i,]<-results %>%
summarise(across(contains("_p_"), ~ mean(.x > 0.05)))
type1error$conditions[i] <- i
}
but I got warning message like this, and the results does not seems to be what I was expected
(summarise results for each row)
Warning messages:
1: In `[<-.data.frame`(`*tmp*`, i, , value = list(a_p_ = 0.5, b_p_ = 0.5, :
provided 3 variables to replace 2 variables
2: In `[<-.data.frame`(`*tmp*`, i, , value = list(a_p_ = 0.75, b_p_ = 0.5, :
provided 3 variables to replace 2 variables
How can I fix this?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
您可以
在2022-05-11创建 reprex package (v2.0.1)
You could do
Created on 2022-05-11 by the reprex package (v2.0.1)
如果您已经在
tidyverse
中工作,我认为发布的答案更加一致,但是这是使用更多基本r函数的选项:If you are already working in the
tidyverse
, I think the answers posted are more consistent, but here is an option using more base R functions: