Reputation: 2859
Assume I have a set of dataframes named N01 to N99. They are identical in structure and all have a column named DATE.
Assume this DATE column is of factor
class and I want to change it to POSIXlt
. If I intend to update the NXX$DATE
column using a function like strptime()
, how can I do so for all N01-N99 dataframes?
I've tried using lapply
but have trouble using assign
in the function to assign to a specific column.
For example, this didn't work:
lapply( ls(pattern="N[0-9][0-9]"), function(x){ assign(paste(get(x),'$DATE',sep=''), strptime(get(x)$DATE),"%y/%m/%d), envir=.GlobalEnv)})
I also tried assigning to paste(x,"['TIME']")
too, but it also created a new data.frame instead of updating the column.
How can I achieve what I want?
Upvotes: 0
Views: 1206
Reputation: 55685
Here is one solution
# collect data frames into a list using mget
dats = mget(sprintf('N%02d', 1:99))
# loop through list of data frames and convert factor to date
dats = lapply(dats, function(x){
x$DATE = strptime(x$DATE, "%y/%m/%d")
return(x)
})
Upvotes: 3
Reputation: 886938
using lapply
with list2env
. But, you could do all the necessary analysis within the lapply
and save each list
element to separate file without even changing the original dataset using list2env
.
lst <- mget(ls(pattern="^N[0-9]+"))
list2env(
lapply(lst, function(x) {x$DATE <- strptime(x$DATE, "%Y-%m-%d") #change here
x}),
envir=.GlobalEnv)
<environment: R_GlobalEnv>
str(N01)
#'data.frame': 5 obs. of 2 variables:
#$ DATE: POSIXlt, format: "2012-01-01" "2012-01-02" ...
#$ val : num -0.212 -1.042 -1.153 0.322 -1.5
set.seed(25)
N01 <- data.frame(DATE = factor(seq(as.Date("2012-01-01"), length.out=5, by=1)), val=rnorm(5))
N02 <- data.frame(DATE = factor(seq(as.Date("2012-01-01"), length.out=5, by=1)), val=rnorm(5))
N25 <- data.frame(DATE = factor(seq(as.Date("2012-01-01"), length.out=5, by=1)), val=rnorm(5))
Upvotes: 2