Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/71.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R根据数据帧中的当前值替换值_R_Replace_Conditional_Dataframe - Fatal编程技术网

R根据数据帧中的当前值替换值

R根据数据帧中的当前值替换值,r,replace,conditional,dataframe,R,Replace,Conditional,Dataframe,我有一个尺寸约为ts1[1002000]的数据框,如下所示: > ts1[1:8, 1:6] DD LEVEL X136747 X136749 X136752 X136753 ... ... 1 D04MX.x LC 0.25 0.30 -0.01 -0.05 2 D08MX.x LC 0.22 0.11 0.11 0

我有一个尺寸约为ts1[1002000]的数据框,如下所示:

> ts1[1:8, 1:6]
       DD LEVEL     X136747     X136749     X136752     X136753     ... ...
1 D04MX.x    LC        0.25        0.30       -0.01       -0.05
2 D08MX.x    LC        0.22        0.11        0.11        0.00
3 D15MX.x    LC        0.31        0.33       -0.23       -0.08
4 D29MX.x    LC        0.28        0.14       -0.28       -0.08
5 D04HX.x    SC        0.11       -0.26       -0.21       -0.33
6 D08HX.x    SC        0.25       -0.23       -0.07       -0.25
7 D15HX.x    SC        0.29        0.03       -0.05       -0.10
8 D29HX.x    SC        0.29        0.13       -0.09        0.02
... ...
我想将名为X ts1[3:ncolts1]的列下所有介于-0.1和0.1之间的值替换为0。我尝试了以下方法:

> ts1 <- ifelse(abs(ts1) < 1, 0, ts1)
Error in Math.data.frame(ts1) : 
  non-numeric variable in data frame: DDLEVEL
> ts1[which(abs(ts1) < 1)] <- 0
Error in Math.data.frame(ts1) : 
  non-numeric variable in data frame: DDLEVEL
> ts1[which(abs(is.numeric(ts1)) < 1)] <- 0
> ts1
  DD LEVEL X1367471_at X1367495_at X1367527_at X1367536_at
1  0    LC        0.25        0.30       -0.01       -0.05
2  0    LC        0.22        0.11        0.11        0.00
3  0    LC        0.31        0.33       -0.23       -0.08
... ...
> ts1 <- ts1[, lapply(.SD[3:ncol(ts1)], ifelse(abs(ts1) < 1, 0, ts1))]
Error in Math.data.frame(ts1) : 
  non-numeric variable in data frame: DDLEVEL

我做错了什么?我确实需要保留前两列。有捷径吗?谢谢。

假设您的数据名为df:


假设您的数据名为df:


非常感谢,成功了。然而,由于2.5x GHz的桌面,在Core2上完成[1646247]的数据帧需要410秒~7分钟。不知有没有更快的办法?为什么我不能使用df[,3:ncoldf],因为有时我可能在另一个列名中有一个X。@user-没有意识到速度会是一个问题。也许有更快的方法,让我想想。对于你的另一个问题,这应该是可行的,尽管不是在一个公司检查中…或者调整你的正则表达式,使其更具选择性。非常感谢,它奏效了。然而,由于2.5x GHz的桌面,在Core2上完成[1646247]的数据帧需要410秒~7分钟。不知有没有更快的办法?为什么我不能使用df[,3:ncoldf],因为有时我可能在另一个列名中有一个X。@user-没有意识到速度会是一个问题。也许有更快的方法,让我想想。至于你的另一个问题,这应该是可行的,虽然不是在一个公司检查…或调整您的正则表达式,以更具选择性。
colsToEdit <- grepl("X", names(df))
df[, colsToEdit][abs(df[, colsToEdit]) <= 0.1] <- 0
 DD LEVEL X136747 X136749 X136752 X136753
1 D04MX.x    LC    0.25    0.30    0.00    0.00
2 D08MX.x    LC    0.22    0.11    0.11    0.00
3 D15MX.x    LC    0.31    0.33   -0.23    0.00
4 D29MX.x    LC    0.28    0.14   -0.28    0.00
5 D04HX.x    SC    0.11   -0.26   -0.21   -0.33
6 D08HX.x    SC    0.25   -0.23    0.00   -0.25
7 D15HX.x    SC    0.29    0.00    0.00    0.00
8 D29HX.x    SC    0.29    0.13    0.00    0.00