pivot_使用mean和sd函数将Summary_的输出延长
我试图总结我的数据集中的所有列(它有许多列,尽管下面的rep示例只有2列),得到每个变量的平均值和sd。我希望输出为长格式pivot_使用mean和sd函数将Summary_的输出延长,r,dplyr,pivot,reshape2,R,Dplyr,Pivot,Reshape2,我试图总结我的数据集中的所有列(它有许多列,尽管下面的rep示例只有2列),得到每个变量的平均值和sd。我希望输出为长格式 #Example dataset d <- iris %>% select(Sepal.Length,Sepal.Width) names(d) <- c("SepalLength","SepalWidth") #Summarizing and trying to make it long s <- d %>% summarize_each(
#Example dataset
d <- iris %>% select(Sepal.Length,Sepal.Width)
names(d) <- c("SepalLength","SepalWidth")
#Summarizing and trying to make it long
s <- d %>% summarize_each( list(mean=mean,sd=sd) ) # has summar stats, but they are in wide format
# trying to pivot.
s %>% pivot_longer( ??? what do I put here ???)
#示例数据集
d%选择(萼片长度,萼片宽度)
名称(d)%轴长(?我在这里放什么?)
我尝试了一些变体(例如:
pivot\u更长(names\u to=“key”,values\u to=“value”)
),但总是收到一条错误消息。我们可以在内部使用选择帮助程序
library(dplyr)
library(tidyr)
s %>%
pivot_longer(everything())
# A tibble: 4 x 2
# name value
# <chr> <dbl>
#1 SepalLength_mean 5.84
#2 SepalWidth_mean 3.06
#3 SepalLength_sd 0.828
#4 SepalWidth_sd 0.436
s %>%
pivot_longer(cols = everything(),
names_to = c(".value", "statistic"), names_sep="_")
# A tibble: 2 x 3
# statistic SepalLength SepalWidth
# <chr> <dbl> <dbl>
#1 mean 5.84 3.06
#2 sd 0.828 0.436
s %>%
pivot_longer(cols = everything(),
names_to = c("colNames", ".value"), names_sep="_")
# A tibble: 2 x 3
# colNames mean sd
# <chr> <dbl> <dbl>
#1 SepalLength 5.84 0.828
#2 SepalWidth 3.06 0.436
s %>%
gather