Order of variable names pivot_wider

左心房为你撑大大i 提交于 2020-08-04 10:20:48

问题


How can you change the order of the variable names in pivot_wider to have he names_from before the values_from?

Using the us_rent_income dataset:

df <- us_rent_income %>% 
  pivot_wider(names_from = NAME,
              values_from = c(estimate, moe))

This gives results like 'estimate_Alabama', how do we change the order of the variable so it is 'Alabama_estimate'?


回答1:


The documentation for pivot_wider() states "If values_from contains multiple values, the value will be added to the front of the output column" so there doesn't seem to be any way to control this as part of the reshape. Instead, it has to be done afterwards.

Assuming there are no other variable names in the dataset that contain _ (if so, the separator can be changed to something unique using the names_sep argument), one approach would be:

library(tidyr)

df <- us_rent_income %>% 
  pivot_wider(names_from = NAME,
              values_from = c(estimate, moe)) %>%
  setNames(nm = sub("(.*)_(.*)", "\\2_\\1", names(.)))

head(names(df))

[1] "GEOID"  "variable"  "Alabama_estimate"  "Alaska_estimate"  "Arizona_estimate"  "Arkansas_estimate"



回答2:


A possible solution:

df <- us_rent_income %>% 
  pivot_wider(names_from = NAME,
              values_from = c(estimate, moe))

names(df) <- sapply(strsplit(names(df), "_"), 
                    function(x) if(length(x)>1) paste0(x[2],"_",x[1]) else x)

names(df)
# [1] "GEOID" "variable" "Alabama_estimate" "Alaska_estimate" 
#     "Arizona_estimate" "Arkansas_estimate" "California_estimate" ...


来源:https://stackoverflow.com/questions/58182388/order-of-variable-names-pivot-wider

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!