如何在 r 中的数据框中替换多行中的值?
how to replace values in multiple rows in a dataframe in r?
我的数据是这样的:
dput(head(CORt_r100, 5))
structure(list(rDate = structure(c(1438019100, 1438019400, 1438019700,
1438020000, 1438020300), class = c("POSIXct", "POSIXt"), tzone = ""),
I630 = c(0.536, 0.506, 0.419, 0.456, 0.427), I800 = c(0.414,
0.388, 0.339, 0.351, 0.331), I532 = c(0.547, 0.534, 0.463,
0.488, 0.464), I570 = c(0.522, 0.508, 0.467, 0.468, 0.445
), WR630 = c(0.0127, 0.0573, 0.0083, 0.0057, 0.0053), WR800 = c(0.0144,
0.0506, 0.0249, 0.0163, 0.0159), WR532 = c(0.0139, 0.0394,
0.006, 0.005, 0.0049), WR570 = c(0.0176, 0.0379, 0.0094,
0.0054, 0.0049), NR630 = c(0.006, 0.034, 0.006, 0.004, 0.004
), NR800 = c(0.007, 0.04, 0.019, 0.02, 0.019), NR532 = c(0.007,
0.072, 0.01, 0.007, 0.007), NR570 = c(0.009, 0.077, 0.008,
0.007, 0.007), ER630 = c(0.0351, 0.0746, 0.0116, 0.0055,
0.0052), ER800 = c(0.0278, 0.0596, 0.03, 0.0324, 0.0303),
ER532 = c(0.04, 0.085, 0.013, 0.008, 0.008), ER570 = c(0.034,
0.083, 0.013, 0.009, 0.008), PotRad = c(NA, NA, NA, 256.497787465489,
NA), Rg = c(NA, NA, NA, 230.782, NA), PotRad.f = c(256.497787465489,
256.497787465489, 256.497787465489, 256.497787465489, 235.488150060979
), Rg.f = c(230.782, 230.782, 230.782, 230.782, 214.260333333333
), REFN532 = c(0.0127970749542962, 0.134831460674157, 0.0215982721382289,
0.014344262295082, 0.0150862068965517), REFN570 = c(0.0172413793103448,
0.151574803149606, 0.0171306209850107, 0.014957264957265,
0.0157303370786517), REFN630 = c(0.0111940298507463, 0.0671936758893281,
0.0143198090692124, 0.0087719298245614, 0.00936768149882904
), REFN800 = c(0.0169082125603865, 0.103092783505155, 0.056047197640118,
0.056980056980057, 0.0574018126888217), REFW532 = c(0.0254113345521024,
0.0737827715355805, 0.0129589632829374, 0.0102459016393443,
0.0105603448275862), REFW570 = c(0.0337164750957854, 0.0746062992125984,
0.0201284796573876, 0.0115384615384615, 0.0110112359550562
), REFW630 = c(0.0236940298507463, 0.113241106719368, 0.0198090692124105,
0.0125, 0.0124121779859485), REFW800 = c(0.0347826086956522,
0.130412371134021, 0.0734513274336283, 0.0464387464387464,
0.048036253776435), REFE532 = c(0.0731261425959781, 0.159176029962547,
0.0280777537796976, 0.0163934426229508, 0.0172413793103448
), REFE570 = c(0.0651340996168582, 0.163385826771654, 0.0278372591006424,
0.0192307692307692, 0.0179775280898876), REFE630 = c(0.0654850746268657,
0.147430830039526, 0.0276849642004773, 0.0120614035087719,
0.0121779859484778), REFE800 = c(0.0671497584541063, 0.15360824742268,
0.0884955752212389, 0.0923076923076923, 0.0915407854984894
), Date = structure(c(16643, 16643, 16643, 16643, 16643), class = "Date")), row.names = c(NA,
5L), class = "data.frame")
我的问题很简单,但我找不到解决方案:
我发现我的数据框中有很多行(行数:222、224、510、512、2238、2239、2240、2526、2527、2528、2814、2815、2816、3103、4255、5980、5981, 5982, 5983) 不满足一个条件。我没有删除行,而是必须用 NA 替换每行中的所有值(数字),但必须维护 rDate
。
我简单地尝试过:
CORt_r100[222, 224, 510, 512, 2238, 2239, 2240, 2526, 2527, 2528, 2814, 2815, 2816, 3103, 4255, 5980, 5981, 5982, 5983, ] <- NA
但没有成功。我尝试了此代码的其他变体,但数据框保持不变。
知道我怎么能做这个手术吗?
提前谢谢你。
我们需要 c
将行索引连接到 vector
并使用 setdiff
指定不需要的列,即 setdiff(names(CORt_r100), "Date")
作为列名
CORt_r100[c(222, 224, 510, 512, 2238, 2239, 2240, 2526, 2527, 2528, 2814, 2815, 2816, 3103, 4255, 5980, 5981, 5982, 5983), setdiff(names(CORt_r100), "Date") ] <- NA
你快到了,你必须连接你的索引,例如 x[c(222, 224, 510),] <- NA
。但是,这不会执行您想要的操作,因为它将 NA 分配给整行,包括日期列。
这是您数据的一小部分
x <- data.frame(
rDate = structure(c(1438019100, 1438019400, 1438019700,
1438020000, 1438020300), class = c("POSIXct", "POSIXt"), tzone = ""),
I630 = c(0.536, 0.506, 0.419, 0.456, 0.427),
I800 = c(0.414, 0.388, 0.339, 0.351, 0.331),
I532 = c(0.547, 0.534, 0.463, 0.488, 0.464),
ER570 = c(0.034, 0.083, 0.013, 0.009, 0.008),
PotRad = c(NA, NA, NA, 256.497787465489, NA),
Rg = c(NA, NA, NA, 230.782, NA))
我们可以将 NA 分配给第 1、2 和 5 行,并指定第二个括号中的列。
x[c(1,2,5),][2:ncol(x)] <- NA
您还可以使用 which
以有趣的方式来表达,找到一个条件,然后执行类似的 NA 替换(对于 运行 示例重新创建 x,因为在前面的示例中我们用 NA 替换了行).在这种情况下,我们正在寻找 ER570 < 0.01 和 I630 < 0.5 的行。
( idx <- which(x$ER570 < 0.01 & x$I630 < 0.5) )
x[idx,][2:ncol(x)] <- NA
我的数据是这样的:
dput(head(CORt_r100, 5))
structure(list(rDate = structure(c(1438019100, 1438019400, 1438019700,
1438020000, 1438020300), class = c("POSIXct", "POSIXt"), tzone = ""),
I630 = c(0.536, 0.506, 0.419, 0.456, 0.427), I800 = c(0.414,
0.388, 0.339, 0.351, 0.331), I532 = c(0.547, 0.534, 0.463,
0.488, 0.464), I570 = c(0.522, 0.508, 0.467, 0.468, 0.445
), WR630 = c(0.0127, 0.0573, 0.0083, 0.0057, 0.0053), WR800 = c(0.0144,
0.0506, 0.0249, 0.0163, 0.0159), WR532 = c(0.0139, 0.0394,
0.006, 0.005, 0.0049), WR570 = c(0.0176, 0.0379, 0.0094,
0.0054, 0.0049), NR630 = c(0.006, 0.034, 0.006, 0.004, 0.004
), NR800 = c(0.007, 0.04, 0.019, 0.02, 0.019), NR532 = c(0.007,
0.072, 0.01, 0.007, 0.007), NR570 = c(0.009, 0.077, 0.008,
0.007, 0.007), ER630 = c(0.0351, 0.0746, 0.0116, 0.0055,
0.0052), ER800 = c(0.0278, 0.0596, 0.03, 0.0324, 0.0303),
ER532 = c(0.04, 0.085, 0.013, 0.008, 0.008), ER570 = c(0.034,
0.083, 0.013, 0.009, 0.008), PotRad = c(NA, NA, NA, 256.497787465489,
NA), Rg = c(NA, NA, NA, 230.782, NA), PotRad.f = c(256.497787465489,
256.497787465489, 256.497787465489, 256.497787465489, 235.488150060979
), Rg.f = c(230.782, 230.782, 230.782, 230.782, 214.260333333333
), REFN532 = c(0.0127970749542962, 0.134831460674157, 0.0215982721382289,
0.014344262295082, 0.0150862068965517), REFN570 = c(0.0172413793103448,
0.151574803149606, 0.0171306209850107, 0.014957264957265,
0.0157303370786517), REFN630 = c(0.0111940298507463, 0.0671936758893281,
0.0143198090692124, 0.0087719298245614, 0.00936768149882904
), REFN800 = c(0.0169082125603865, 0.103092783505155, 0.056047197640118,
0.056980056980057, 0.0574018126888217), REFW532 = c(0.0254113345521024,
0.0737827715355805, 0.0129589632829374, 0.0102459016393443,
0.0105603448275862), REFW570 = c(0.0337164750957854, 0.0746062992125984,
0.0201284796573876, 0.0115384615384615, 0.0110112359550562
), REFW630 = c(0.0236940298507463, 0.113241106719368, 0.0198090692124105,
0.0125, 0.0124121779859485), REFW800 = c(0.0347826086956522,
0.130412371134021, 0.0734513274336283, 0.0464387464387464,
0.048036253776435), REFE532 = c(0.0731261425959781, 0.159176029962547,
0.0280777537796976, 0.0163934426229508, 0.0172413793103448
), REFE570 = c(0.0651340996168582, 0.163385826771654, 0.0278372591006424,
0.0192307692307692, 0.0179775280898876), REFE630 = c(0.0654850746268657,
0.147430830039526, 0.0276849642004773, 0.0120614035087719,
0.0121779859484778), REFE800 = c(0.0671497584541063, 0.15360824742268,
0.0884955752212389, 0.0923076923076923, 0.0915407854984894
), Date = structure(c(16643, 16643, 16643, 16643, 16643), class = "Date")), row.names = c(NA,
5L), class = "data.frame")
我的问题很简单,但我找不到解决方案:
我发现我的数据框中有很多行(行数:222、224、510、512、2238、2239、2240、2526、2527、2528、2814、2815、2816、3103、4255、5980、5981, 5982, 5983) 不满足一个条件。我没有删除行,而是必须用 NA 替换每行中的所有值(数字),但必须维护 rDate
。
我简单地尝试过:
CORt_r100[222, 224, 510, 512, 2238, 2239, 2240, 2526, 2527, 2528, 2814, 2815, 2816, 3103, 4255, 5980, 5981, 5982, 5983, ] <- NA
但没有成功。我尝试了此代码的其他变体,但数据框保持不变。 知道我怎么能做这个手术吗? 提前谢谢你。
我们需要 c
将行索引连接到 vector
并使用 setdiff
指定不需要的列,即 setdiff(names(CORt_r100), "Date")
作为列名
CORt_r100[c(222, 224, 510, 512, 2238, 2239, 2240, 2526, 2527, 2528, 2814, 2815, 2816, 3103, 4255, 5980, 5981, 5982, 5983), setdiff(names(CORt_r100), "Date") ] <- NA
你快到了,你必须连接你的索引,例如 x[c(222, 224, 510),] <- NA
。但是,这不会执行您想要的操作,因为它将 NA 分配给整行,包括日期列。
这是您数据的一小部分
x <- data.frame(
rDate = structure(c(1438019100, 1438019400, 1438019700,
1438020000, 1438020300), class = c("POSIXct", "POSIXt"), tzone = ""),
I630 = c(0.536, 0.506, 0.419, 0.456, 0.427),
I800 = c(0.414, 0.388, 0.339, 0.351, 0.331),
I532 = c(0.547, 0.534, 0.463, 0.488, 0.464),
ER570 = c(0.034, 0.083, 0.013, 0.009, 0.008),
PotRad = c(NA, NA, NA, 256.497787465489, NA),
Rg = c(NA, NA, NA, 230.782, NA))
我们可以将 NA 分配给第 1、2 和 5 行,并指定第二个括号中的列。
x[c(1,2,5),][2:ncol(x)] <- NA
您还可以使用 which
以有趣的方式来表达,找到一个条件,然后执行类似的 NA 替换(对于 运行 示例重新创建 x,因为在前面的示例中我们用 NA 替换了行).在这种情况下,我们正在寻找 ER570 < 0.01 和 I630 < 0.5 的行。
( idx <- which(x$ER570 < 0.01 & x$I630 < 0.5) )
x[idx,][2:ncol(x)] <- NA