gpt4 book ai didi

r - 根据条件修改列中的值?

转载 作者:行者123 更新时间:2023-12-04 18:16:46 26 4
gpt4 key购买 nike

我知道这是一个非常愚蠢的问题,但我不知道该怎么做。
如果它们大于 299,我想从 trial1 列的每一行的值中减去 300。

我试过了:

sums[sums$trial1 > 299, ][,"trial1"] -= 300

但没有用。到目前为止,我设法让它工作的唯一方法是使用 subset 拆分 data.frame然后修改它:
sums$trial1 = sums$trial1 - 300

然后使用 rbind() .我很确定使用子集和 rbind 是矫枉过正的,但我还没有找到直接的方法......

我使用 dput 来获取我的 data.frame 的样本。
structure(list(part_no = c(10L, 10L, 10L, 10L, 10L, 10L), trial1 = c(294L, 
296L, 298L, 300L, 302L, 304L), trial2 = c(295L, 297L, 299L, 301L,
303L, 305L), id1 = c(1.5, 1.5, 1.5, 2, 2, 2), id2 = c(1.5, 1.5,
1.5, 2, 2, 2), dist1 = c(141L, 141L, 115L, 126L, 177L, 141L),
width1 = c(77L, 77L, 63L, 42L, 59L, 47L), dist2 = c(143L,
135L, 146L, 255L, 327L, 369L), width2 = c(78L, 74L, 80L,
85L, 109L, 123L), ttime1 = c(1752L, 1681L, 1664L, 1798L,
1664L, 1697L), ttime2 = c(2563L, 1849L, 2067L, 1933L, 2118L,
2245L), no_clicks1 = c(8L, 8L, 8L, 8L, 8L, 8L), no_clicks2 = c(8L,
8L, 8L, 8L, 8L, 8L), no_ontarget1 = c(7L, 8L, 8L, 8L, 8L,
8L), no_ontarget2 = c(8L, 8L, 8L, 4L, 7L, 8L), e1 = c(1L,
0L, 0L, 0L, 0L, 0L), e2 = c(0L, 0L, 0L, 4L, 1L, 0L), rating = c(252,
252, 252, 252, 252, 252), prat = c(0.8, 0.8, 0.8, 0.8, 0.8,
0.8), ptim = c(-46.2899543378995, -9.9940511600238, -24.21875,
-7.5083426028921, -27.2836538461538, -32.2922804949912),
ptdiff = c(-47.0899543378995, -10.7940511600238, -25.01875,
-8.3083426028921, -28.0836538461538, -33.0922804949912),
pdist = c(-1.41843971631206, 4.25531914893617, -26.9565217391304,
-102.380952380952, -84.7457627118644, -161.702127659574),
pddiff = c(-2.21843971631206, 3.45531914893617, -27.7565217391304,
-103.180952380952, -85.5457627118644, -162.502127659574),
perr = c(100, NaN, NaN, -Inf, -Inf, NaN), pediff = c(99.2,
NaN, NaN, -Inf, -Inf, NaN)), .Names = c("part_no", "trial1",
"trial2", "id1", "id2", "dist1", "width1", "dist2", "width2",
"ttime1", "ttime2", "no_clicks1", "no_clicks2", "no_ontarget1",
"no_ontarget2", "e1", "e2", "rating", "prat", "ptim", "ptdiff",
"pdist", "pddiff", "perr", "pediff"), row.names = 148:153, class = "data.frame")

谢谢!

最佳答案

您的第一次尝试很接近,但没有 -=基本 R 中的运算符,因此您还需要在右侧提供子集。

sums[sums$trial1 > 299,"trial1"] <- sums[sums$trial1 > 299,"trial1"]-300

关于r - 根据条件修改列中的值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11503434/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com