简体   繁体   English

在tidyr :: gather中使用多个键

[英]use multiple key in tidyr::gather

I used to use reshape2::melt to perform the following, since I want to learn tidyr , I wonder how this can be done 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'))

Thanks! 谢谢!

Try 尝试

iris %>%
  as_tibble %>%
  mutate(country=rep(LETTERS[1:3],ceiling(n()/3))[1:n()]) %>%
  gather(., key = variable , value = value, -Species,-country)

This will gather all columns but Species and country (specified by the minus signs) and call the column containing the variable names 'variable' (the key ) and the entry 'value' (the value ). 这将收集“物种和国家/地区”(由负号指定)以外的所有列,并调用包含变量名称“ variable”( key )和条目“ value”( value )的列。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM