Michael Spector
Michael Spector

Reputation: 113

change column values in list of dataframes in R

I have a list of 12 dataframes. the name of the list is kvish_1_10t.tables . each data frame has a column "day_mean" (always the 7's column in all the dataframes). its impotant to say that all the dataframes look exacly the same. this is an example of one of the tables:

 X2014_kvish_1_10t
    kvish keta maslul yom nefah                date day_mean
1       1   10      1   1  1936 2014-09-07 00:00:00 2910.958
2       1   10      1   1   966 2014-09-07 01:00:00 2910.958
3       1   10      1   1   737 2014-09-07 02:00:00 2910.958
4       1   10      1   1   596 2014-09-07 03:00:00 2910.958
5       1   10      1   1   479 2014-09-07 04:00:00 2910.958
6       1   10      1   1   765 2014-09-07 05:00:00 2910.958
7       1   10      1   1  2082 2014-09-07 06:00:00 2910.958
8       1   10      1   1  3624 2014-09-07 07:00:00 2910.958
9       1   10      1   1  3847 2014-09-07 08:00:00 2910.958
10      1   10      1   1  2960 2014-09-07 09:00:00 2910.958
11      1   10      1   1  2871 2014-09-07 10:00:00 2910.958
12      1   10      1   1  3149 2014-09-07 11:00:00 2910.958
13      1   10      1   1  3615 2014-09-07 12:00:00 2910.958
14      1   10      1   1  3943 2014-09-07 13:00:00 2910.958
15      1   10      1   1  4079 2014-09-07 14:00:00 2910.958
16      1   10      1   1  4856 2014-09-07 15:00:00 2910.958
17      1   10      1   1  5010 2014-09-07 16:00:00 2910.958
18      1   10      1   1  4783 2014-09-07 17:00:00 2910.958
19      1   10      1   1  4684 2014-09-07 18:00:00 2910.958
20      1   10      1   1  4478 2014-09-07 19:00:00 2910.958
21      1   10      1   1  3610 2014-09-07 20:00:00 2910.958
22      1   10      1   1  2799 2014-09-07 21:00:00 2910.958
23      1   10      1   1  2346 2014-09-07 22:00:00 2910.958
24      1   10      1   1  1648 2014-09-07 23:00:00 2910.958
25      1   10      1   2  1145 2014-09-08 00:00:00 2745.917
26      1   10      1   2   671 2014-09-08 01:00:00 2745.917
...
168 rows total

Now, I changed the "day_mean" column (the 7's column in the right) so the values in the location of the 1, 25 ,49 ,73, 97 ,121, 145 seq(1, 168 , 24) place will remain as they are, and the rest will become NA's . so I wrote this to define a vector of numbers which will represent the locations in the "day_mean" column that will get the NA values:

aa = seq(1, 168 , 24) 
bb = rep(T, 168)
bb[aa] = F
cc= (which(bb))


X2014_kvish_1_10t[,7][cc] = NA

Now, as you see, I changed my "day_mean" column so only the relevant values will remain as they are and the rest will become NA's. like here:

> X2014_kvish_1_10t
    kvish keta maslul yom nefah                date day_mean
1       1   10      1   1  1936 2014-09-07 00:00:00 2910.958
2       1   10      1   1   966 2014-09-07 01:00:00       NA
3       1   10      1   1   737 2014-09-07 02:00:00       NA
4       1   10      1   1   596 2014-09-07 03:00:00       NA
5       1   10      1   1   479 2014-09-07 04:00:00       NA
6       1   10      1   1   765 2014-09-07 05:00:00       NA
7       1   10      1   1  2082 2014-09-07 06:00:00       NA
8       1   10      1   1  3624 2014-09-07 07:00:00       NA
9       1   10      1   1  3847 2014-09-07 08:00:00       NA
10      1   10      1   1  2960 2014-09-07 09:00:00       NA
11      1   10      1   1  2871 2014-09-07 10:00:00       NA
12      1   10      1   1  3149 2014-09-07 11:00:00       NA
13      1   10      1   1  3615 2014-09-07 12:00:00       NA
14      1   10      1   1  3943 2014-09-07 13:00:00       NA
15      1   10      1   1  4079 2014-09-07 14:00:00       NA
16      1   10      1   1  4856 2014-09-07 15:00:00       NA
17      1   10      1   1  5010 2014-09-07 16:00:00       NA
18      1   10      1   1  4783 2014-09-07 17:00:00       NA
19      1   10      1   1  4684 2014-09-07 18:00:00       NA
20      1   10      1   1  4478 2014-09-07 19:00:00       NA
21      1   10      1   1  3610 2014-09-07 20:00:00       NA
22      1   10      1   1  2799 2014-09-07 21:00:00       NA
23      1   10      1   1  2346 2014-09-07 22:00:00       NA
24      1   10      1   1  1648 2014-09-07 23:00:00       NA
25      1   10      1   2  1145 2014-09-08 00:00:00 2745.917
26      1   10      1   2   671 2014-09-08 01:00:00       NA
27      1   10      1   2   497 2014-09-08 02:00:00       NA
...
168 rows total

So far everithing went good, but when I'm trying to do this same action for all the dataframes in my list, it falis. I tried to write the following command but it didnt went good, I created a function that all the 7's columns in each dataframe will get the new values:

func = function(x) (x[,7][cc] = NA)

lapply(kvish_1_10t.tables, func)

How can I change all my day_mean columns in each dataframe ?

Upvotes: 5

Views: 2784

Answers (2)

Parfait
Parfait

Reputation: 107567

Simply add a return() in your function:

func <- function(X) {
    X[,7][cc] <- NA   

    return(X)
}

new_df_list <- lapply(kvish_1_10t.tables, func)

Upvotes: 4

Jay
Jay

Reputation: 452

I was hoping someone would post an answer using lapply, but seeing as how no answers have been posted and I am much better at for loops, I thought I would at least post this in hopes that it addresses your immediate problem:

d1<-data.frame(y1<-c(1,2,3),y2<-c(4,5,6))
d2 <- data.frame(y1=c(3,2,1),y2=c(6,5,4))
myl <- list(d1, d2)
cc <- c(1,3)
for (n in 1:length(myl)){
  myl[[n]][cc,2] <- NA
  print(myl[[n]][cc,2])
}

So for your specific example, I think this should work (assuming kvish_1_10t.tables really is a list--take a look at the structure using str(kvish_1_10t.tables) if you are unsure:

for (n in 1:length(kvish_1_10t.tables)){
 kvish_1_10t.tables[[n]][cc,"day_mean"] <- NA
}

Upvotes: 0

Related Questions