我是R编程的新手,我想“清理”我找到的数据。这是数据框,我想从变量Month:
设置一个ts() Month gas
Jan 1973 79
Feb 1973 79
Mar 1973 77
Apr 1973 73
May 1973 77
我想这样:
Month gas
1973 79
1973 79
1973 77
1973 73
1973 77
感谢您的宝贵帮助。
答案 0 :(得分:1)
我们可以使用sub
df1$Month <- as.numeric(sub("^\\S+\\s+", '', df1$Month))
df1$Month
#[1] 1973 1973 1973 1973 1973
如果要提取的字符数相同,则更快的方法是substr
with(df1, as.numeric(substr(Month, nchar(Month)-3, nchar(Month))))
#[1] 1973 1973 1973 1973 1973
如果&#34;月&#34;是yearmon
对象(来自zoo
)
library(lubridate)
year(df1$Month)
#[1] 1973 1973 1973 1973 1973
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))
答案 1 :(得分:1)
我认为您的Month
是chr
类型,gas
整数。这将删除空格前的所有月份名称。
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