I want to tidy my data with the gather function but how do I specify multiple columns at once?
Say this is my data:
Country Country.Code Year X0tot4 X5tot9 X10tot14 X15tot19 X20tot24
1 Viet Nam 704 1955 4606 2924 2389 2340 2502
2 Viet Nam 704 1960 5842 4410 2860 2356 2318
3 Viet Nam 704 1965 6571 5646 4328 2823 2335
4 Viet Nam 704 1970 7065 6391 5548 4271 2797
5 Viet Nam 704 1975 7658 6862 6237 5437 4208
6 Viet Nam 704 1980 7991 7473 6754 6113 5266
7 Viet Nam 704 1985 8630 7855 7375 6657 6027
8 Viet Nam 704 1990 9212 8513 7770 7277 6571
9 Viet Nam 704 1995 9200 9099 8447 7702 7140
10 Viet Nam 704 2000 7245 9119 9053 8402 7610
11 Viet Nam 704 2005 6760 7140 8997 8951 8257
12 Viet Nam 704 2010 7277 6657 7015 8891 8775
13 Viet Nam 704 2015 7753 7233 6623 6982 8817
Now I want to create one new column Age.groups
with the variables from X0tot4
to X20tot24
.
Something like df %>% gather(key = "Age.group", value = c(4:8))
. Console says this is not the right column specification. But what is?
In the
gather
function,value
specifies the name of value column in the result; To specify which columns to gather, you can usestart_column:end_column
syntax, this will gather all columns from the start_column to end_column; In your case, it would beX0tot4:X20tot24
:Nice answer by Psidom. Alternatively, you can exclude columns using "-".
We can also specify the columns to
gather
by matching a string. The following will all work.