热线电话:13121318867

登录
2018-11-05 阅读量: 736
关于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')]
0.0000
1
关注作者
收藏
评论(0)

发表评论

暂无数据
推荐帖子