Change column values ​​in data list in R

I have a list of 12 frames. the name of the list is kvish_1_10t.tables . each data frame has a "day_mean" column (always 7 columns in all data frames). its impotant to say that all dataframes look 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
      

Run code


Now I have changed the column "day_mean" (7 columns on the right), so the values ​​at location 1, 25, 49, 73, 97, 121, 145 seq (1, 168, 24) the place will remain as it is and the rest will become NA ... so I wrote this to define a vector of numbers that will represent the locations in the "day_mean" column that will get 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 can see, I have changed my table "day_mean" so that only the corresponding values ​​will remain as they are, and the rest will become NA. 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
      

Run code


Everything worked out well so far, but when I try to do the same action for all dataframes in my list, it's falis. I tried to write the following command but it didn't go well, I created a function where all 7 columns in each framework will get 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 ?

+3


source to share


2 answers


I was hoping someone would post an answer using lappie, but seeing how no answers were posted and I am much better at loops, I thought I would at least post this, hope 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 is indeed a list - look at the structure using str (kvish_1_10t.tables) if you're not sure:

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

      

0


source


Just add return()

to your function:



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

    return(X)
}

new_df_list <- lapply(kvish_1_10t.tables, func)

      

0


source







All Articles