Fabricio Carboni
Fabricio Carboni

Reputation: 15

R - How to convert Date fields from 3 datasets with for loop?

I'm trying to convert date field of 3 datasets using for loop and got the error:

Error in as.Date.default(mydatasets[i]$Date, "%Y-%m-%d") : do not know how to convert 'mydatasets[i]$Date' to class “Date”

Here's the code:

papel_BBD <-read.csv("http://ichart.finance.yahoo.com/table.csv?s=BBD",sep=",", header=1)
papel_HSBC <-read.csv("http://ichart.finance.yahoo.com/table.csv?s=HSBC",sep=",", header=1)
papel_ITAU <-read.csv("http://ichart.finance.yahoo.com/table.csv?s=ITUB",sep=",", header=1)


mydatasets <- c(papel_BBD, papel_HSBC, papel_ITAU)

for (i in 1:length(mydatasets)) {
  mydatasets[i]$Date <- as.Date(mydatasets[i]$Date, "%Y-%m-%d")
}

Console:

for (i in 1:length(mydatasets)) {
  mydatasets[i]$Date <- as.Date(mydatasets[i]$Date, "%Y-%m-%d")
}

Error in as.Date.default(mydatasets[i]$Date, "%Y-%m-%d") : do not know how to convert 'mydatasets[i]$Date' to class “Date”

I think I'm missing something in this line inside the for loop:

mydatasets[i]$Date <- as.Date(mydatasets[i]$Date, "%Y-%m-%d")

Thanks

Upvotes: 0

Views: 74

Answers (2)

narendra-choudhary
narendra-choudhary

Reputation: 4816

Try this

for(i in length(mydatasets)){
    if(is.factor(mydatasets[[i]])){
        mydatasets[[i]] <- as.Date(mydatasets[[i]])
    }
}

str(mydatasets)

## $ Date     : Date[1:3346], format: "2015-11-13" "2015-11-12" "2015-11-11" "2015-11-10" ...
## $ Open     : num [1:3346] 5.64 5.61 5.81 5.5 5.67 5.69 5.69 5.82 5.51 5.48 ...
## $ High     : num [1:3346] 5.68 5.78 5.81 5.75 5.71 5.83 5.85 5.86 5.86 5.55 ...
## $ Low      : num [1:3346] 5.51 5.59 5.63 5.41 5.53 5.61 5.65 5.61 5.51 5.38 ...
## $ Close    : num [1:3346] 5.58 5.64 5.81 5.65 5.6 5.83 5.81 5.67 5.83 5.53 ...
## $ Volume   : int [1:3346] 15207900 11032800 19484900 13652300 17659600 11074400 12065900 16797400 24521100 17566100 ...
## $ Adj.Close: num [1:3346] 5.58 5.64 5.81 5.65 5.6 ...
## $ Date     : Date[1:4111], format: "2015-11-13" "2015-11-12" "2015-11-11" "2015-11-10" ...
## $ Open     : num [1:4111] 39.1 39.8 39.9 39.5 40.3 ...
## ...

Upvotes: 0

akrun
akrun

Reputation: 887571

We can use lapply with the correct format (based on the urls) for the 'Date' column.

lst <- lapply(mget(ls(pattern='^papel\\_.*')), transform, 
               Date=as.Date(Date))
lapply(lst, head, 2)
#$papel_BBD
#        Date Open High  Low Close   Volume Adj.Close
#1 2015-11-13 5.64 5.68 5.51  5.58 15207900      5.58
#2 2015-11-12 5.61 5.78 5.59  5.64 11032800      5.64

#$papel_HSBC
#        Date  Open  High   Low Close  Volume Adj.Close
#1 2015-11-13 39.07 39.18 38.85 38.92 1922600     38.92
#2 2015-11-12 39.75 39.86 39.53 39.57 2020700     39.57

#$papel_ITAU
#        Date Open High  Low Close   Volume Adj.Close
#1 2015-11-13 7.36 7.41 7.11  7.14 15996600      7.14
#2 2015-11-12 7.36 7.51 7.30  7.36 14638300      7.36

It is better to keep the datasets in the list itself. If we need to reflect the changes in the original dataset, we can use list2env (not recommended though)

list2env(lst, envir=.GlobalEnv)

str(papel_BBD)
#'data.frame':   3346 obs. of  7 variables:
# $ Date     : Date, format: "2015-11-13" "2015-11-12" ...
# $ Open     : num  5.64 5.61 5.81 5.5 5.67 5.69 5.69 5.82 5.51 5.48 ...
# $ High     : num  5.68 5.78 5.81 5.75 5.71 5.83 5.85 5.86 5.86 5.55 ...
# $ Low      : num  5.51 5.59 5.63 5.41 5.53 5.61 5.65 5.61 5.51 5.38 ...
# $ Close    : num  5.58 5.64 5.81 5.65 5.6 5.83 5.81 5.67 5.83 5.53 ...
# $ Volume   : int  15207900 11032800 19484900 13652300 17659600 11074400 12065900 16797400 24521100 17566100 ...
# $ Adj.Close: num  5.58 5.64 5.81 5.65 5.6 ...

Upvotes: 1

Related Questions