使用循环基于其他列修改 R 中的几列数据



我正在尝试遍历几列数据,并根据另一列中的行更新一列中的行。

library(tidyverse)
df <- tibble("A" = c(rnorm(10)), "B" = c (rnorm(10)), "AA" = c(rnorm(10)), "BB" = c(rnorm(10)))
df
# A tibble: 10 x 4
A       B     AA     BB
<dbl>   <dbl>  <dbl>  <dbl>
1  0.278  -1.21   -0.892 -1.07 
2 -3.39   -0.0315  0.418  0.514
3 -2.37   -0.374   0.504 -1.14 
4 -0.642   0.590   1.24  -0.186
5  1.48   -0.888  -0.610 -0.454
6 -2.06    1.43   -0.197 -1.19 
7  0.314   0.628  -1.36  -1.73 
8 -0.122  -0.480   1.62   2.47 
9 -0.0693  1.20   -0.498 -0.858
10  0.751  -0.389  -0.340  0.743

我想将 A 列设置为 NA,AA 列是 <0。 B列和BB列等也是如此。

我有一个一次适用于一列的 ifelse 语句,但我需要将其放入 purrr 中的循环或 map(( 函数中以连续遍历所有列。但是每次我尝试将其放入for循环或使用map_dbl((,都会出错。 这是我的工作ifelse声明。

df <- mutate(df, A = ifelse(AA < 0, NA, A))

这基本上是我尝试循环的方式:

这基本上就是我尝试这样做的方式。 我是R的新手,所以我可能完全关闭了。

cleaning <- function(df,x,y,z) {df <- mutate(df, df$x == ifelse(df$y < 2, NA, df$z)) 
for (i in seq_along(x)) {df[i] <-cleaning[[i]]}
}
cleaning(df,A:B,AA:BB,A:B)

这是我实际数据的前 10 行的 dput。 AF3:AF4 列需要根据值为 <2 的 CQ_AF3:CQ_AF4 进行更新。

structure(list(ID = c(74L, 11L, 66L, 125L, 89L, 25L, 57L, 43L, 
114L, 47L), COUNTER = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L
), INTERPOLATED = c(4518.461428, 4573.333222, 4604.102452, 4655.384502, 
4570.256298, 4473.846044, 4610.256298, 4585.128093, 4721.538346, 
4653.84604), AF3 = c(3624.615296, 4025.640927, 4034.871696, 4004.615287, 
3971.281954, 3868.717854, 3968.205031, 4005.128107, 3898.974264, 
4058.461439), F7 = c(4345.128099, 4644.615271, 4665.128091, 4525.640915, 
4571.281939, 4804.615267, 4479.48707, 4614.358862, 4482.563993, 
4708.205013), F3 = c(3757.948626, 3978.974262, 4057.435798, 4118.974258, 
4061.538362, 3591.794784, 4060.512721, 4019.999902, 4203.589641, 
4068.717849), FC5 = c(4107.179387, 4126.153745, 4058.97426, 4085.640926, 
4098.461438, 4245.128101, 4094.358874, 4133.333232, 3742.051191, 
4152.307591), T7 = c(4316.410151, 4824.102446, 4765.128089, 4783.076806, 
4685.640911, 4422.051174, 4742.051166, 4710.769116, 4850.256292, 
4734.358859), P7 = c(4747.179371, 4458.97425, 4423.589635, 4497.948608, 
4578.974247, 4752.307576, 4418.46143, 4599.999888, 4713.333218, 
4579.487068), O1 = c(3947.179391, 3908.205033, 3966.66657, 4042.051183, 
4008.20503, 4006.153748, 3972.820416, 3984.615287, 4167.692206, 
3996.410159), O2 = c(4077.435798, 4171.281949, 4094.358874, 4147.179386, 
4121.538361, 4138.461437, 4137.948617, 4151.79477, 4134.358873, 
4118.974258), P8 = c(3606.666578, 3820.512727, 3874.35888, 4060.512721, 
3775.897344, 3631.794783, 3959.999903, 3896.410161, 3858.974265, 
3922.051186), T8 = c(4146.666565, 4330.256304, 4353.333227, 4415.384507, 
4338.461432, 3941.538365, 4432.307584, 4382.051175, 4587.692196, 
4419.999892), FC6 = c(4418.974251, 4632.8204, 4692.307578, 4634.871682, 
4568.717837, 4684.61527, 4363.589637, 4615.897323, 4654.358861, 
4631.794759), F4 = c(3808.205035, 4205.640923, 4373.846047, 4414.871687, 
4293.846049, 3727.692217, 4151.79477, 4218.461435, 4284.61528, 
4341.538355), F8 = c(4282.051177, 4243.076819, 4239.999896, 4437.435789, 
4340.512714, 3809.743497, 4276.922972, 4269.743485, 4536.922966, 
4360.512714), AF4 = c(487L, 484L, 513L, 444L, 444L, 0L, 0L, 0L, 
482L, 0L), RAW_CQ = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), 
CQ_AF3 = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), CQ_F7 = c(4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), CQ_F3 = c(4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L), CQ_FC5 = c(4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L), CQ_T7 = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L), CQ_P7 = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L), CQ_O1 = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), CQ_O2 = c(0L, 
0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L), CQ_P8 = c(4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L), CQ_T8 = c(4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L), CQ_FC6 = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L, 4L, 4L), CQ_F4 = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 
4L), CQ_F8 = c(4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), CQ_AF4 = c(4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L), CQ_CMS = c(4L, 4L, 4L, 
4L, 4L, 4L, 4L, 4L, 4L, 4L), CQ_DRL = c(1768L, 1770L, 1767L, 
1768L, 1768L, 1768L, 1768L, 1771L, 1767L, 1770L), GYROX = c(1515L, 
1517L, 1511L, 1512L, 1516L, 1514L, 1514L, 1515L, 1515L, 1515L
), `GYROY MARKER` = c(0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 0L, 
0L)), row.names = c(NA, -10L), class = c("tbl_df", "tbl", 
"data.frame"))

这个怎么样?此df创建set.seed(123)

> df
# A tibble: 10 x 4
A      B      AA      BB
<dbl>  <dbl>   <dbl>   <dbl>
1  1.26   1.66  -0.675   0.183 
2  1.52  -1.63  -1.16   -0.0934
3  0.346  0.890  2.02    0.368 
4  0.723 -0.532 -2.37   -0.580 
5 -0.434  0.158  2.23    0.553 
6  1.57   0.385  0.511   1.84  
7 -0.516  1.25  -0.765  -2.05  
8  0.561  0.313  0.0828 -0.214 
9 -0.717 -1.46  -0.408  -0.166 
10 -1.84  -0.267 -0.414  -1.05  

然后修改后的 df:

> df %>% mutate_at(vars(A), funs(ifelse(AA < 0, NA, .))) %>% 
mutate_at(vars(B), funs(ifelse(BB < 0, NA, .)))
# A tibble: 10 x 4
A       B     AA      BB
<dbl>   <dbl>  <dbl>   <dbl>
1  NA       1.22  -1.07   0.426 
2  NA      NA     -0.218 -0.295 
3  NA       0.401 -1.03   0.895 
4  NA       0.111 -0.729  0.878 
5  NA      -0.556 -0.625  0.822 
6  NA       1.79  -1.69   0.689 
7   0.461   0.498  0.838  0.554 
8  -1.27   NA      0.153 -0.0619
9  NA      NA     -1.14  -0.306 
10  -0.446  NA      1.25  -0.380 

如果您需要保存修改后的df,只需将newname <-放在此命令的前面即可。

无需加载外部库的简单 for 循环解决方案:

> df
A           B          AA           BB
1  -1.13879933 -0.05219939  1.40599762  0.277708500
2  -1.60341369 -1.23928624  0.25177871  0.009469529
3           NA  0.44474461 -0.68301589  1.914665838
4           NA          NA -0.49642122 -0.555400536
5  -2.83194181          NA  0.09429098 -0.324189223
6   1.06254068          NA  0.84104522 -1.121906866
7   0.04871273          NA  1.13978470 -1.527878009
8           NA          NA -1.29621577 -0.818883608
9   0.43346025          NA  0.45678599 -0.673519674
10 -0.08255017          NA  0.83648472 -1.851880253

法典

for(i in 1:ncol(df)){
for(j in 1:ncol(df))(
if(paste0(colnames(df[i]), colnames(df[i])) == colnames(df[j]))(
for(k in 1:nrow(df))(
if(df[k, j] < 0)(
df[k, i] <- NA
)
)    
)
)    
}

数据

set.seed(1701)
df <- data.frame("A" = c(rnorm(10)), "B" = c (rnorm(10)),
"AA" = c(rnorm(10)), "BB" = c(rnorm(10)))

====

==========================================================

奖励:真实数据的注释代码

# Iterate through all columns for base i and match j
for(i in 1:ncol(df)){
for(j in 1:ncol(df))(
# Check if name of base + prefix is the same as match
if(paste0("CQ_", colnames(df[i])) == colnames(df[j]))(
# Iterate through rows
for(k in 1:nrow(df))(
# Check value in match row
if(df[k, j] < 2)(
# Update value in same row of base
df[k, i] <- NA
) 
)    
)
)    
}

这适用于tibblesdata.frames。如果您有NA数据,则还应该使用!is.na()或其他东西来排除这些情况,否则值检查会抱怨。

最新更新