-1

I have a data frame with the following columns:

**Columns   Country   Year   Number_of_deaths**
**Data**      US      2000    25
              US      2001    30
              UK      2000    30
              UK      2001    21

I want to convert this to the following format:

**Columns: Country   2000   2001   2002   2003   2004**
**Data**     US        25     30     35     40     25
             UK        30     21     21     23     45

Can somebody give me sample code in R to do this? Any package is fine. Your help will be appreciated.

Moon_Watcher
  • 416
  • 1
  • 6
  • 19

3 Answers3

3

A small illustration as requested:

library(tidyr)
# creating sample data
dt = data.frame(country = rep(LETTERS[1:2], each=2),
                year = 2000:2003,
                num = c(25,30,30,21))
dt %>% spread(year, num)
#   country 2000 2001 2002 2003
# 1       A   25   30   NA   NA
# 2       B   NA   NA   30   21
KFB
  • 3,501
  • 3
  • 15
  • 18
2

use this :

hope it works

library(reshape2)
dcast(data,country~year,value.var="No_of_deaths")

Output:

  country 2000 2001
1      UK   30   21
2      US   25   30

Thanks

PKumar
  • 10,971
  • 6
  • 37
  • 52
1

Here is a way using base R

 res <- reshape(df, timevar="Year", idvar="Country", direction="wide")
 colnames(res) <- gsub(".*\\.", "",colnames(res)) #if you need `colnames` as `year` alone.  But, it is not good to have `numeric` column names.


  res
  # Country  2000 2001
  #1      US   25   30
  #3      UK   30   21

If you are using $ then make sure to use backticks

  res$`2000`
  #[1] 25 30

data

  df <- structure(list(Country = c("US", "US", "UK", "UK"), Year = c(2000L, 
  2001L, 2000L, 2001L), Number_of_deaths = c(25L, 30L, 30L, 21L
  )), .Names = c("Country", "Year", "Number_of_deaths"), class = "data.frame", row.names = c(NA, 
 -4L))
akrun
  • 874,273
  • 37
  • 540
  • 662