gpt4 book ai didi

r - R中数据帧的智能转置

转载 作者:行者123 更新时间:2023-12-04 10:20:24 28 4
gpt4 key购买 nike

我在 R 中有多个数字列 (mydata = summary(labo[sapply(mydata, is.numeric)]))然后执行数据框转换 mydata<-as.data.frame(mydata)我把它作为“数据有”:

Var1         Var2           Freq    
1 cars Min. : 1.100
2 cars 1st Qu.: 3.375
3 cars Median : 4.500
4 cars Mean :12.075
5 cars 3rd Qu.:12.350
6 cars Max. :12.000
7 cars NA's :3
8 bikes Min. : 12.00
9 bikes 1st Qu.: 23.00
10 bikes Median : 12.00
11 bikes Mean : 10.14
12 bikes 3rd Qu.: 12.00
13 bikes Max. :12.00
14 bikes NA's :2
15 wheels Min. :10.00
16 wheels 1st Qu.:12.00
17 wheels Median :10.00
18 wheels Mean :10.54
19 wheels 3rd Qu.:12.00
20 wheels Max. :20.00
21 wheels NA's :3

我正在寻找一种巧妙地将数据帧的输出转换为:

数据要:

Var2 ! Min ! 1st Qu. ! Median ! 3rd Qu. ! Max. !  NA's 
cars !1.100! 3.375. .....
bikes!12.00! 23.00......

最佳答案

一种选择是 pivot_wider

library(dplyr)
library(tidyr)
df1 %>%
separate(Freq, into = c('VarN', 'Freq'), sep=":\\s*", convert = TRUE) %>%
select(-Var1) %>%
pivot_wider(names_from = VarN, values_from = Freq)
数据
mydata <- summary(iris[sapply(iris, is.numeric)])
df1 <- as.data.frame(mydata)

关于r - R中数据帧的智能转置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60892236/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com