Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

change column values in list of dataframes in R

Tags:

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 ?

like image 450
Michael Spector Avatar asked Jul 26 '17 00:07

Michael Spector


People also ask

How do I replace a value in a list in R?

replace() function in R Language is used to replace the values in the specified string vector x with indices given in list by those given in values. It takes on three parameters first is the list name, then the index at which the element needs to be replaced, and the third parameter is the replacement values.

How do I change a column value in a DataFrame in R?

To replace a column value in R use square bracket notation df[] , By using this you can update values on a single column or on all columns. To refer to a single column use df$column_name . The following example updates Orange St with Portola Pkwy on the address column. To update values on all columns.

How do I change the values in a column in a DataFrame?

Suppose that you want to replace multiple values with multiple new values for an individual DataFrame column. In that case, you may use this template: df['column name'] = df['column name']. replace(['1st old value', '2nd old value', ...], ['1st new value', '2nd new value', ...])


1 Answers

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)
like image 195
Parfait Avatar answered Oct 11 '22 12:10

Parfait