R中数据帧中的楼层值
您好,我想在我的R中数据帧中的楼层值,r,floor,R,Floor,您好,我想在我的datdataframe中建立一个最小值(0.3): col1 col2 col3 col4 0 0.2 0.8 1 0.2 0 0.7 2.1 13.2 12.1 1.58 0 我的预期产出: col1 col2 col3 col4 0.3 0.3 0.8 1 0.3 0.3 0.7 2.1 13.2 12.1 1.58 0.3 干杯 您可以使用la
dat
dataframe中建立一个最小值(0.3):
col1 col2 col3 col4
0 0.2 0.8 1
0.2 0 0.7 2.1
13.2 12.1 1.58 0
我的预期产出:
col1 col2 col3 col4
0.3 0.3 0.8 1
0.3 0.3 0.7 2.1
13.2 12.1 1.58 0.3
干杯 您可以使用
lappy
来执行此操作
dat[] <- lapply(dat, function(x) ifelse(x<0.3, 0.3, x))
> dat
col1 col2 col3 col4
1 0.3 0.3 0.80 1.0
2 0.3 0.3 0.70 2.1
3 13.2 12.1 1.58 0.3
您可以按如下方式使用
lappy
:
> dat[1:4] <- lapply(dat[1:4], function(x) ifelse(x<0.3, 0.3, x)) #only columns 1:4 will be changed
> dat #the id column is as it was and you still have a data.frame
col1 col2 col3 col4 id
1 0.3 0.3 0.80 1.0 a
2 0.3 0.3 0.70 2.1 b
3 13.2 12.1 1.58 0.3 c
>dat[1:4]您可以使用lappy
来执行此操作
dat[] <- lapply(dat, function(x) ifelse(x<0.3, 0.3, x))
> dat
col1 col2 col3 col4
1 0.3 0.3 0.80 1.0
2 0.3 0.3 0.70 2.1
3 13.2 12.1 1.58 0.3
您可以按如下方式使用lappy
:
> dat[1:4] <- lapply(dat[1:4], function(x) ifelse(x<0.3, 0.3, x)) #only columns 1:4 will be changed
> dat #the id column is as it was and you still have a data.frame
col1 col2 col3 col4 id
1 0.3 0.3 0.80 1.0 a
2 0.3 0.3 0.70 2.1 b
3 13.2 12.1 1.58 0.3 c
>dat[1:4]这里的矢量化最好。由于我们没有任何因子
或字符
列,因此我们可以将dat
视为矩阵
,并直接替换值
## dat <- read.table(h=T, text = 'col1 col2 col3 col4
## 0 0.2 0.8 1
## 0.2 0 0.7 2.1
## 13.2 12.1 1.58 0')
> min <- 0.3
> dat[dat < min] <- min
> dat
## col1 col2 col3 col4
## 1 0.3 0.3 0.80 1.0
## 2 0.3 0.3 0.70 2.1
## 3 13.2 12.1 1.58 0.3
##dat min dat[dat
这里的矢量化效果最好。由于我们没有任何因子
或字符
列,因此我们可以将dat
视为矩阵
,并直接替换值
## dat <- read.table(h=T, text = 'col1 col2 col3 col4
## 0 0.2 0.8 1
## 0.2 0 0.7 2.1
## 13.2 12.1 1.58 0')
> min <- 0.3
> dat[dat < min] <- min
> dat
## col1 col2 col3 col4
## 1 0.3 0.3 0.80 1.0
## 2 0.3 0.3 0.70 2.1
## 3 13.2 12.1 1.58 0.3
##dat min dat[dat
或dat[]或结构(数据帧(pmax(dat,min))
。但是你是不是通过这种方式得到了NA
值?我是。或dat[]或结构(数据帧(pmax(dat,min))
。但是你是不是通过这种方式得到了NA
值?我是。