我有下表:
country | country code | variable | 1990 | 1991 | 1992 | 1993 | ...
-----------|--------------|-----------------|------|------|------|------|-----
Argentina | ARG | employment in a | .40 | .35 | .37 | .43 | ...
Argentina | ARG | employment in b | .35 | .30 | .32 | .29 | ...
Argentina | ARG | employment in c | .25 | .35 | .31 | .28 | ...
-
Brazil | BRA | employment in a | .45 | .35 | .33 | .44 | ...
Brazil | BRA | employment in b | .30 | .33 | .34 | .27 | ...
Brazil | BRA | employment in c | .25 | .32 | .33 | .29 | ...
... | ... | ... | ... | ... | ... | ... | ...
但是我需要将其更改为以下格式:
country | country code | year | employment in a | employment in b | employment in c
-----------|--------------|------|-----------------|-----------------|-----------------
Argentina | ARG | 1990 | .40 | .35 | .25
Argentina | ARG | 1991 | .35 | .30 | .35
Argentina | ARG | 1992 | .37 | .32 | .31
Argentina | ARG | 1993 | .43 | .29 | .28
Argentina | ARG | ... | ... | ... | ...
-
Brazil | BRA | 1990 | .45 | .30 | .25
Brazil | BRA | 1991 | .35 | .33 | .32
Brazil | BRA | 1992 | .33 | .34 | .33
Brazil | BRA | 1993 | .44 | .27 | .29
Brazil | BRA | ... | ... | ... | ...
... | ... | ... | ... | ... | ...
我有更多的变量,国家和年份,但是表格应该足以说明问题。
我尝试了gather()
和reshape()
,但无法解决我的问题。
有人可以帮我解决这个问题吗?
答案 0 :(得分:2)
这必须是重复的,但是当我找到合适的欺骗目标时,可以使用tidyverse
和gather
+ spread
来解决
library(tidyverse)
df %>% gather(year, val, -(1:3)) %>% spread(variable, val)
## A tibble: 8 x 6
# country country_code year employment_in_a employment_in_b employment_in_c
# <chr> <chr> <chr> <dbl> <dbl> <dbl>
#1 Argentina ARG 1990 0.4 0.35 0.25
#2 Argentina ARG 1991 0.35 0.3 0.35
#3 Argentina ARG 1992 0.37 0.32 0.31
#4 Argentina ARG 1993 0.43 0.290 0.28
#5 Brazil BRA 1990 0.45 0.3 0.25
#6 Brazil BRA 1991 0.35 0.33 0.32
#7 Brazil BRA 1992 0.33 0.34 0.33
#8 Brazil BRA 1993 0.44 0.27 0.290
df <- read_table2("country country_code variable 1990 1991 1992 1993
Argentina ARG employment_in_a .40 .35 .37 .43
Argentina ARG employment_in_b .35 .30 .32 .29
Argentina ARG employment_in_c .25 .35 .31 .28
Brazil BRA employment_in_a .45 .35 .33 .44
Brazil BRA employment_in_b .30 .33 .34 .27
Brazil BRA employment_in_c .25 .32 .33 .29 ")