0

I want to revise the values of a variable. The values are for a series of years. They start from 1960 and end at 2017. There are multiple 1960s, 1961s and so on till 2017. The multiple values for each year correspond to different countries. Countries are another variable in another column. However, each year is tagged with an X. eg. each 1960 has X1960 and so on till X2017. I want to remove the X for all years.

database is as shown below

Country           Year      GDP
Afghanistan       X1960
England           X1960
Sudan             X1960
.
.
.
Afghanistan       X2017
England           X2017
Sudan             X2017
.
.
MrFlick
  • 195,160
  • 17
  • 277
  • 295
Rye
  • 9
  • 1

2 Answers2

1

Hi You can you gsub function to your data frame

ABC <- data.frame(country = c("Afghanistan", "England"), year = c("X1960","X1960"))

print(ABC)
      country  year
1 Afghanistan X1960
2     England X1960

ABC$year <- gsub("X","",ABC$year)

> print(ABC)
      country year
1 Afghanistan 1960
2     England 1960
Arun kumar mahesh
  • 2,289
  • 2
  • 14
  • 22
0

Here's a tidyverse solution.

# Load libraries
library(dplyr)
library(readr)

# Dummy data frame
df <- data.frame(country = c("Afghanistan", "England", "Sudan"), 
                 year = rep("X1960", 3),
                 stringsAsFactors = FALSE)

# Quick peak
print(df)
#>       country  year
#> 1 Afghanistan X1960
#> 2     England X1960
#> 3       Sudan X1960

# Strip all non-numerics from strings
df %>% mutate(year = parse_number(year))
#>       country year
#> 1 Afghanistan 1960
#> 2     England 1960
#> 3       Sudan 1960

Created on 2019-05-23 by the reprex package (v0.2.1)

Dan
  • 11,370
  • 4
  • 43
  • 68