在 tidyr::gather 中使用多个键
use multiple key in tidyr::gather
我以前用reshape2::melt
来执行以下操作,因为我想学习tidyr
,我想知道如何在tidyr::gather
中做到这一点:
iris %>%
as_tibble %>%
mutate(country=rep(LETTERS[1:3],ceiling(n()/3))[1:n()]) %>%
reshape2::melt(.,id.var=c('country','Species'))
谢谢!
尝试
iris %>%
as_tibble %>%
mutate(country=rep(LETTERS[1:3],ceiling(n()/3))[1:n()]) %>%
gather(., key = variable , value = value, -Species,-country)
这将收集除 Species 和 country(由减号指定)以外的所有列,并调用包含变量名称 'variable'(key
)和条目 'value' 的列(value
).
我以前用reshape2::melt
来执行以下操作,因为我想学习tidyr
,我想知道如何在tidyr::gather
中做到这一点:
iris %>%
as_tibble %>%
mutate(country=rep(LETTERS[1:3],ceiling(n()/3))[1:n()]) %>%
reshape2::melt(.,id.var=c('country','Species'))
谢谢!
尝试
iris %>%
as_tibble %>%
mutate(country=rep(LETTERS[1:3],ceiling(n()/3))[1:n()]) %>%
gather(., key = variable , value = value, -Species,-country)
这将收集除 Species 和 country(由减号指定)以外的所有列,并调用包含变量名称 'variable'(key
)和条目 'value' 的列(value
).