[英]Recode values in R
如果x> 1但<2,我想重新编码一列中的值,它将重新编码为1
这是我的代码:
neu$b <- lapply(neu$swl.y, function(x) ifelse(x>1 & x<=2, 1, x))
那里有错吗?
swl.y
2.2
1.2
3.4
5.6
我实际上需要重新编码所有值:
neu$c <- with(neu, ifelse(swl.y>1 & swl.y <=2, 1, swl.y))
neu$c <- with(neu, ifelse(swl.y>2 & swl.y <=3, 2, swl.y))
neu$c <- with(neu, ifelse(swl.y>3 & swl.y <=4, 3, swl.y))
neu$c <- with(neu, ifelse(swl.y>4 & swl.y <=5, 4, swl.y))
neu$c <- with(neu, ifelse(swl.y>5 & swl.y <=6, 5, swl.y))
neu$c <- with(neu, ifelse(swl.y>6 & swl.y <=7, 6, swl.y))
我想我知道问题出在哪里。 当R运行第二行代码时,重新编码的值恢复为先前的值。
我们不需要为单个列循环。 通过使用lapply(neu$swl.y
,我们可以将列中的每个元素用作list
元素,而我们可能不需要这些元素ifelse
函数是矢量化的,可以通过逻辑将其直接用于列“ swl.y” OP的帖子中提到的情况。
neu$b <- with(neu, ifelse(swl.y>1 & swl.y <=2, 1, swl.y))
否则,我们将“ b”列创建为“ swl.y”,并根据逻辑条件更改“ b”的值。
neu$b <- neu$swl.y
neu$b[with(neu, swl.y>1 & swl.y <=2)] <- 1
为了更好地理解OP代码的问题,我们可以检查lapply
的输出
lapply(neu$swl.y, function(x) x) #similar to `as.list(neu$swl.y)`
#[[1]]
#[1] 3
#[[2]]
#[1] 0
#[[3]]
#[1] 0
#[[4]]
#[1] 2
#[[5]]
#[1] 1
输出是一个list
,其中列的每个元素都作为list
元素。 在列表上使用ifelse
可能不是最佳方法,因为它已经过矢量化处理(如上所述)。 但是,假设我们是否使用ifelse
lapply(neu$swl.y, function(x) ifelse(x>1 & x<=2, 1, x))
#[[1]]
#[1] 3
#[[2]]
#[1] 0
#[[3]]
#[1] 0
#[[4]]
#[1] 1
#[[5]]
#[1] 1
可以将data.frame
视为具有相同长度列表元素的list
。 因此,基于上面的输出,这应该是一个5列1行的data.frame。 通过单列“ b”,我们创建了一个包含5个列表元素的list
列。
neu$b <- lapply(neu$swl.y, function(x) ifelse(x>1 & x<=2, 1, x))
str(neu)
#'data.frame': 5 obs. of 2 variables:
#$ swl.y: int 3 0 0 2 1
#$ b :List of 5
# ..$ : int 3
# ..$ : int 0
# ..$ : int 0
# ..$ : num 1
# ..$ : int 1
但是,这不是我们想要的。 有什么补救办法? 一种方法是使用sapply/vapply
而不是lapply
,因为长度相同, lapply
将返回vector
输出,或者我们不unlist
lapply
输出以创建vector
neu$b <- sapply(neu$swl.y, function(x) ifelse(x>1 & x<=2, 1, x))
str(neu)
#'data.frame': 5 obs. of 2 variables:
# $ swl.y: int 3 0 0 2 1
# $ b : num 3 0 0 1 1
根据OP的编辑过的帖子,如果我们需要多次重新编码,请使用cut
或findInterval
。 在cut
,我们可以指定breaks
,还有其他参数labels
可以返回默认标签。
with(neu1, cut(swl.y, breaks=c(-Inf,1,2,3,4,5,6,Inf), labels=F)-1)
#[1] 2 1 3 5
set.seed(48)
neu <- data.frame(swl.y=sample(0:5, 5, replace=TRUE))
#newdata
neu1 <- structure(list(swl.y = c(2.2, 1.2, 3.4, 5.6)),
.Names = "swl.y", class = "data.frame", row.names = c(NA, -4L))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.