Home > database >  How to subset dataframe based on multiple conditions?
How to subset dataframe based on multiple conditions?

Time:01-20

I have a dataframe df covering information from year 1998 to year 2020 like this

Country Indicator 1998 1999 2000 2001 2002 2003 ... 2020 
[1,] A a 1 2 3 4 5 6 ... 13
[2,] A b 7 8 9 10 11 12 ... 19
[3,] A c 13 14 15 16 17 18 ... 25

To capture data from year 2001 to year 2020, I have tried to subset a dataframe by column names and execute df_subset <- df[, c('Country, 'Indicator', '2001', '2002', '2003', '2004', ...','2020)]

  1. However, I'm not sure whether there is an easier way to subset this dataframe by column names and the sequence of columns? e.g., combine df[,c('Country','Indicator')] with df[,6:25]
  2. I want to transform df and make col. And this is my expected output
Country Year a b c
A 2001 1 2 3
A 2002 7 8 9
A 2003 13 14 15

Thanks for your any help in advance!

CodePudding user response:

You should do:

reshape2::recast(df ,Country   variable ~ Indicator)
  •  Tags:  
  • Related