在R中的数据框列表中更改列值 [英] change column values in list of dataframes in R
问题描述
我有一个12个数据帧的列表.列表的名称为 kvish_1_10t.tables .每个数据帧都有一列"day_mean"(在所有数据帧中始终为7列).说所有数据帧看起来都完全相同是很重要的.这是其中一个表的示例:
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
现在,我更改了"day_mean"列(右侧的7列),以便将1、25,49,73,97,121,145 seq(1,168,24)位置将保持不变,其余位置将变为NA的位置.因此,我编写此代码的目的是定义一个数字向量,该向量将表示将获得NA值的"day_mean"列中的位置:
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
现在,如您所见,我更改了"day_mean"列,因此只有相关的值将保持原样,其余的将变为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
到目前为止,一切都进展顺利,但是当我尝试对列表中的所有数据框执行相同的操作时,它就失败了.我尝试编写以下命令,但效果不佳,我创建了一个函数,每个数据帧中的所有7列都将获得新值:
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)
如何更改每个数据框中的所有 day_mean 列?
How can I change all my day_mean columns in each dataframe ?
推荐答案
只需在函数中添加 return()
:
func <- function(X) {
X[,7][cc] <- NA
return(X)
}
new_df_list <- lapply(kvish_1_10t.tables, func)
这篇关于在R中的数据框列表中更改列值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!