简体   繁体   中英

Reading CSV file in R and formatting dates and time while reading and avoiding missing values marked as?

I am trying to Reading CSV file in R . How can I read and format dates and times while reading and avoid missing values marked as ?. The data I load after reading should be clean.

I tried something like data <- read.csv("Data.txt") It worked, but the dates and times were as is.

Also how can I extract a subset of data from specific data range?

For this I tried something like

subdata <- subset(data, 
                  Date== 01/02/2007 & Date==02/02/2007, 
                  select = Date:Sub_metering_3)

I get error Error in eval(expr, envir, enclos) : object 'Date' not found

Date is the first column.

The functions read.csv() and read.table() are not set up to do detailed fancy conversion of things like dates that can have many formats. When these functions don't automatically do what's wanted, I find it best to read the data in as text and then convert variables after the fact.

data <- read.csv("Data.txt",colClasses="character",na.strings="?")
data$FixedDate <- as.Date(data$Date,format="%Y/%m/%d")

or whatever your date format is. The variable FixedDate will then be of type Date and you can use equality and other conditions to subset.

Also, in your example code you are putting 01/02/2007 as bare code, which results in dividing 1 by 2 and then by 2007 yielding 0.0002491281, rather than inserting a meaningful date. Consider as.Date("2007-01-02") instead.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM