我正在使用RStudio弄乱R中的某些列,并试图将其中一列的数据类型从Char更改为Date。
我使用了一些选项,而最接近的是
data$Date <- as.Date(as.character(data$Date))
尽管这似乎也不起作用,因为它会将列的值更改为一些奇怪的值
即来自
类似
我不太清楚为什么转换不起作用。
这是我到目前为止的代码
# load the tidyverse library
library("tidyverse")
setwd("C:/Users/ibrahim.cetinkaya/OneDrive - NTT/Desktop/data")
##################### Part A #####################
# data files (you need to specify the paths of the CSV files (e.g. relativeor absolute) )
files <- c("data/201808.csv",
"data/201809.csv",
"data/201810.csv",
"data/201811.csv",
"data/201812.csv",
"data/201901.csv",
"data/201902.csv",
"data/201903.csv",
"data/201904.csv",
"data/201905.csv",
"data/201906.csv",
"data/201908.csv"
)
#Concatenate into one data frame.
data <- data.frame()
for (i in 1:length(files)){
temp <- read_csv(files[i], skip = 7)
data <- rbind(data, temp)
}
#View to verify
view(data)
#Part 2
#Remove vairables which have no data at all (All the data are na's)
#Remove variables that doesn't have adequate data (70% of the number of records are NA's)
data <- data[rowMeans(is.na(data))<=0.9,]
view(data)
#Change the column names to have no spaces between the words
names(data) <- gsub(" ", "_", names(data))
view(data)
#Convert Date to date type
#df2 <- data %>% mutate_at(vars(data), as.Date, format="%m-%d-%Y")
#data %>% mutate(data$Date==as.Date(Date, format = "%m.%d.%Y"))
data$Date <- as.Date(as.character(data$Date))
#^^^ This doesn't seem to be working properly ^^^
#Checking if it worked
typeof(data$Date)
view(data)
任何建议将不胜感激。 我希望能够更改数据类型,然后提取月份并将其用于对框架中的其他一些数据进行分组。
答案 0 :(得分:2)
使用
data$Date <- as.Date(data$date, "%m/%d/%Y")
然后提取月份
data$Month <- format(data$Date, "%m")
我们也可以使用lubridate
data$date <- lubridate::mdy(data$date)
,然后使用month
提取月份。
data$month <- month(data$date)
并带有anytime
data$Date <- anytime::anydate(data$Date)