简体   繁体   中英

xts convert data frame to character

I have a csv file and extract data using

banknifty <- as.xts(read.zoo("banknifty.csv",sep=",",tz="" ,header=T))

read.zoo() extracts the data frame with numeric values but as I apply as.xts() , the data. frame data. frame 's numeric values get converted to characters.

# banknifty[1,] gives 
2008-01-01 09.34:00 "10" "12" "13"

I want as.xts should return data.frame with numeric values. How to avoid this problem?

You're confused about the nature of xts/zoo objects. They are matrices with an ordered index attribute, therefore you cannot mix types in xts/zoo objects like you can in a data.frame.

The reason your object is being converted to character is because some of the values in your file are not numeric. This is also why you get the NAs introduced by coercion error when you tried hd1's solution.

So the answer to your question is, "fix your CSV file", but we can't help you fix it unless you show us the file's contents.

Use as.numeric and your code will be:

> data.in <- as.xts(read.zoo("banknifty.csv",sep=",",tz="" ,header=T);


> sapply(c(1:4), function(n) { data.in[,n] <- as.numeric(data.in[,n]) }, simplify=TRUE )
        [,1]    [,2]    [,3]    [,4]
[1,] 6032.25 6040.50 6032.17 6036.29
[2,] 6036.29 6036.29 6020.00 6025.05
[3,] 6025.05 6026.00 6020.10 6023.12
[4,] 6023.12 6034.45 6022.73 6034.45
[5,] 6034.45 6034.45 6030.00 6030.00
[6,] 6030.00 6038.00 6028.25 6038.00
> data.in
                         V2      V3      V4      V5
2007-01-02 10:00:00 6032.25 6040.50 6032.17 6036.29
2007-01-02 10:05:00 6036.29 6036.29 6020.00 6025.05
2007-01-02 10:10:00 6025.05 6026.00 6020.10 6023.12
2007-01-02 10:15:00 6023.12 6034.45 6022.73 6034.45
2007-01-02 10:20:00 6034.45 6034.45 6030.00 6030.00
2007-01-02 10:25:00 6030.00 6038.00 6028.25 6038.00
> 
> sapply(c(1:4), function(n) { data.in[,n] <- as.numeric(data.in[,n]) }, simplify=TRUE ) 

This command does not make any change to data.in. It returns the data in same format with quotes

> data.in
                         V2      V3      V4      V5
2007-01-02 10:00:00 "6032.25" "6040.50" "6032.17" "6036.29"
2007-01-02 10:05:00 "6036.29" "6036.29" "6020.00" "6025.05"
2007-01-02 10:10:00 "6025.05" "6026.00" "6020.10" "6023.12"

I just ran into a similar problem. In my case, the issue was that the as.xts() function tries to convert the date column along with the numeric columns. Because R does not consider dates to be numeric values, it automatically converts the entire data frame to character. I'm assuming that happens in your example as well (you can check this using your .csv-file).

Something like this should help:

data.in <- read.csv("banknifty.csv",sep=",",header=T)
data.in[,1] <- format(as.Date(data.in[,1]), format="%Y-%m-%d", tz="GMT", usetz=TRUE) #change tz to whatever applies
data.in[,1] <- as.POSIXct(data.in[,1], "GMT")
data.ts <- xts(data.in[,c(2,3,4,5)], order.by = data.in[,1]) 

(Note that data.ts <- xts(data.in, order.by = data.in[,1]) would replicate the unwanted conversion. Also, apologies that this is probably not the cleanest / most concise code, I'm still learning.)

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