1

I have several lists of dataframes and I want to format the date in each single dataframe within all lists of dataframes. Here is an example code:

v1 = c("2000-05-01", "2000-05-02", "2000-05-03", "2000-05-04", "2000-05-05")
v2 = seq(2,20, length = 5)
v3 = seq(-2,7, length = 5)
v4 = seq(-6,3, length = 5)

df1 = data.frame(Date = v1, df1_Tmax = v2, df1_Tmean = v3, df1_Tmin = v4)
dfl1 <- list(df1, df1, df1, df1)
names(dfl1) = c("ABC_1", "DEF_1", "GHI_1", "JKL_1")

v1 = c("2000-05-01", "2000-05-02", "2000-05-03", "2000-05-04", "2000-05-05")
v2 = seq(3,21, length = 5)
v3 = seq(-3,8, length = 5)
v4 = seq(-7,4, length = 5)

df2 = data.frame(Date = v1, df2_Tmax = v2, df2_Tmean = v3, df2_Tmin = v4)
dfl2 <- list(df2, df2, df2, df2)
names(dfl2) = c("ABC_2", "DEF_2", "GHI_2", "JKL_2")

v1 = c("2000-05-01", "2000-05-02", "2000-05-03", "2000-05-04", "2000-05-05")
v2 = seq(4,22, length = 5)
v3 = seq(-4,9, length = 5)
v4 = seq(-8,5, length = 5)

df3 = data.frame(Date = v1, df3_Tmax = v2, df3_Tmean = v3, df3_Tmin = v4)
dfl3 <- list(df3, df3, df3, df3)
names(dfl3) = c("ABC_3", "DEF_3", "GHI_3", "JKL_3")

v1 = c("2000-05-01", "2000-05-02", "2000-05-03", "2000-05-04", "2000-05-05")
v2 = seq(2,20, length = 5)
v3 = seq(-2,8, length = 5)
v4 = seq(-6,3, length = 5)

abc = data.frame(Date = v1, ABC_Tmax = v2, ABC_Tmean = v3, ABC_Tmin = v4)

abclist <-list(abc, abc, abc, abc)

names(abclist) = c("ABC_abc", "DEF_abc", "GHI_abc", "JKL_abc")

I know how to change the date-column manually:

dfl1$ABC_1$Date = as.Date(dfl1$ABC_1$Date,format="%Y-%m-%d")
class(dfl1$ABC_1$Date)

But how can I do that for each single Date-Column in all of my lists of dataframes?

2 Answers 2

2

Here is one option using get and assign

nms <- c('dfl1', 'dfl2', 'dfl3', 'abclist')
lapply(nms, function(x) assign(x,lapply(get(x), 
                                 function(y) {y$Date1 <- as.Date(y$Date, format="%Y-%m-%d") 
                                 return(y)}), 
                        envir = .GlobalEnv))

PS: Be careful with assign since it will change your global environment .GlobalEnv. Many R users will suggest the list solution over assign.

Sign up to request clarification or add additional context in comments.

4 Comments

Why is that so much of a problem? What are the risks?
Oh I see the problem now. It adds another columns with the updated Date. That is actually not that good.
@Essi I just add Date1 to see the difference you can do y$Date <- as.Date(y$Date, format="%Y-%m-%d").
2

This can be done with lapply:

lapply(dfl1, function(x) {
  x$Date <- as.Date(x$Date, format="%Y-%m-%d") 
  return(x)})

If you want to do this for all of you df-lists you need to store them in a list and then you can use a slightly modified version of the above call:

df_list <- list(dfl1, dfl2, dfl3, abclist)

lapply(df_list, function(x) {
  x[[1]]$Date <- as.Date(x[[1]]$Date, format="%Y-%m-%d") 
  return(x)})

This assumes that the Date-column has always the same name "Date".

8 Comments

Can you please show me how to do that for all of my dataframe lists? That was actually my question.
Yes, sorry the last part got lost when I first wrote the answer.
Thank you! I see that this command stores all lists in one big list. What do I have to do to get my original files again? I mean all lists seperated with the same names as before, but the updated date-columns?
You could apply the first version to each df list separately, i.e. use dfl2 instead of dfl1 in the first call. Having the same column name should not be a problem as the dfs are stored in different list entries...
You can reassign them: dfl1 <- df_list$dfl1 and so on, but then I guess you are more looking for the solution from A. Suliman...
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.