change column values in list of dataframes in R -
i have list of 12 dataframes. name of list kvish_1_10t.tables . each data frame has column "day_mean" (always 7's column in dataframes). impotant dataframes exacly same. example of 1 of 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, changed "day_mean" column (the 7's column in right) values in location of 1, 25 ,49 ,73, 97 ,121, 145 seq(1, 168 , 24) place remain are, , rest become na's . wrote define vector of numbers represent locations in "day_mean" column 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, see, changed "day_mean" column relevant values remain , rest become na's. 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, when i'm trying same action dataframes in list, falis. tried write following command didnt went good, created function 7's columns in each dataframe new values:
func = function(x) (x[,7][cc] = na) lapply(kvish_1_10t.tables, func)
how can change day_mean columns in each dataframe ?
i hoping post answer using lapply, seeing how no answers have been posted , better @ loops, thought @ least post in hopes addresses 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) (n in 1:length(myl)){ myl[[n]][cc,2] <- na print(myl[[n]][cc,2]) }
so specific example, think should work (assuming kvish_1_10t.tables list--take @ structure using str(kvish_1_10t.tables) if unsure:
for (n in 1:length(kvish_1_10t.tables)){ kvish_1_10t.tables[[n]][cc,"day_mean"] <- na }
Comments
Post a Comment