啊啊啊啊啊吖

2018-11-05   阅读量: 620

数据分析师 R语言

关于gather函数应用的问题

扫码加入数据分析学习群

原来的数据格式

or.png

希望的数据格式

de.png

请问如何用gather函数进行宽变长的变换,谢谢

数据:

structure(list(conc = c(0, 0.05), HMP = c(71.014, 71.5105), hmpSD = c(2.16233,

0.44194), PP = c(71.014, 72.975), PPSD = c(2.16233, 1.20491),

    TPP = c(71.014, 72.43), TPPSD = c(2.16233, 1.24305)), .Names = c("conc",

"HMP", "hmpSD", "PP", "PPSD", "TPP", "TPPSD"), row.names = c(NA,

-2L), class = c("tbl_df", "tbl", "data.frame"))

"conc" "HMP" "hmpSD" "PP" "PPSD" "TPP" "TPPSD"

0 71.014 2.16233 71.014 2.16233 71.014 2.16233

0.05 71.5105 0.44194 72.975 1.20491 72.43 1.24305

library(dplyr)

library(tidyr)

library(stringr)



data <- structure(list(conc = c(0, 0.05), HMP = c(71.014, 71.5105), hmpSD = c(2.16233,

0.44194), PP = c(71.014, 72.975), PPSD = c(2.16233, 1.20491),

    TPP = c(71.014, 72.43), TPPSD = c(2.16233, 1.24305)), .Names = c("conc",

"HMP", "hmpSD", "PP", "PPSD", "TPP", "TPPSD"), row.names = c(NA,

-2L), class = c("tbl_df", "tbl", "data.frame"))



gather(data, key = type, value = hard, HMP, PP, TPP) %>%

gather(key = type2, value = sd, hmpSD, PPSD, TPPSD) %>%

filter(str_to_upper(type2) %>% gsub('SD','',.)==type) %>%

.[,c('conc','type','hard','sd')]
添加CDA认证专家【维克多阿涛】,微信号:【cdashijiazhuang】,提供数据分析指导及CDA考试秘籍。已助千人通过CDA数字化人才认证。欢迎交流,共同成长!
0.0000 0 1 关注作者 收藏

评论(0)


暂无数据

推荐课程

推荐帖子