matteo mazzarano
matteo mazzarano

Reputation: 3

How do I erase first letters of cells in data frame of a variable?

I am new in R programming, and i would like to "clean" data I found. This is the data frame and I would like to set up a ts() from the variable Month:

        Month     gas
        Jan 1973  79
        Feb 1973  79
        Mar 1973  77
        Apr 1973  73
        May 1973  77

I would like to have it like this:

   Month gas
   1973  79
   1973  79
   1973  77
   1973  73
   1973  77

Thanks for your precious help.

Upvotes: 0

Views: 44

Answers (2)

bim
bim

Reputation: 829

I assumed your Month is chr type, and gas integer. This will delete all the month name stand before the space.

Month <- c("Jan 1973","Feb 1973", "Mar 1973", "Apr 1973", "May 1973")
gas <- c(79,79,77,73,77)
df <- data.frame(Month, gas, stringsAsFactors = FALSE)
# print(df)
df$Year <- unlist(strsplit(df$Month, split=" ", fixed=TRUE))[2]
df$Month <- NULL #delete old column
df <- data.frame(c(df[2], df[1])) # rearranged data frame
print(df)

# Year gas
# 1 1973  79
# 2 1973  79
# 3 1973  77
# 4 1973  73
# 5 1973  77

Upvotes: 1

akrun
akrun

Reputation: 887048

We can use sub

df1$Month <-  as.numeric(sub("^\\S+\\s+", '', df1$Month))
df1$Month
#[1] 1973 1973 1973 1973 1973

Or a slighly faster approach would be substr if the number of characters to extract are the same

 with(df1, as.numeric(substr(Month, nchar(Month)-3, nchar(Month))))
 #[1] 1973 1973 1973 1973 1973

If the "Month" is yearmon object (from zoo)

 library(lubridate)
 year(df1$Month)
 #[1] 1973 1973 1973 1973 1973

data

df1 <- structure(list(Month = c("Jan 1973", "Feb 1973", 
 "Mar 1973", 
"Apr 1973", "May 1973"), gas = c(79L, 79L, 77L, 73L, 
77L)), .Names = c("Month", 
"gas"), class = "data.frame", row.names = c(NA, -5L))

Upvotes: 1

Related Questions