问题描述
我的数据框
City Ind: Money Position Money Ind: Health Position Health
NY 6 5 4 3
LA 2 1 9 8
BA 7 11 13 15
所需的数据帧
Ind Position_NY Position_LA Position_BA Ind_NY Ind_LA Ind_BA
Money 5 1 11 6 2 7
Health 3 8 15 4 9 13
谢谢
解决方法
这对您有帮助吗?
library(tidyverse)
df <- tribble(
~City,~`Ind: Money`,~`Position Money`,~`Ind: Health`,~`Position Health`,"NY",6,5,4,3,"LA",2,1,9,8,"BA",7,11,13,15
)
data <- pivot_longer(df,cols = starts_with(c("Ind","Position"))) %>%
separate(name,c("var","Ind")) %>%
pivot_wider(names_from = c("City","var"))