过滤我的 R 数据框中的特定列
Filtering a specific column in my R dataframe
我想用以下代码在 R 中创建一个 for 循环。我尝试了很多方法,但没有用。有人知道如何在没有重复代码的情况下为此创建一个 for 循环吗?抱歉,如果这是一个非常愚蠢的问题。
#x[,4]
#[1] 234 788 211 350 532 98 77 45 123 900 135 841 283 15 421 19 643 934
以上是我要筛选的列的一部分。
我想用下面的代码过滤它。
xmin_15_xmax_100 <- x[which(x[,4] >= 15 & x[,4] <= (100)),]
xmin_15_xmax_200 <- x[which(x[,4] >= 15 & x[,4] <= (200)),]
xmin_15_xmax_300 <- x[which(x[,4] >= 15 & x[,4] <= (300)),]
xmin_15_xmax_400 <- x[which(x[,4] >= 15 & x[,4] <= (400)),]
xmin_15_xmax_500 <- x[which(x[,4] >= 15 & x[,4] <= (500)),]
xmin_15_xmax_600 <- x[which(x[,4] >= 15 & x[,4] <= (600)),]
xmin_15_xmax_700 <- x[which(x[,4] >= 15 & x[,4] <= (700)),]
xmin_15_xmax_800 <- x[which(x[,4] >= 15 & x[,4] <= (800)),]
xmin_15_xmax_900 <- x[which(x[,4] >= 15 & x[,4] <= (900)),]
xmin_15_xmax_1000 <- x[which(x[,4] >= 15 & x[,4] <= (1000)),]
xmin_20_xmax_100 <- x[which(x[,4] >= 20 & x[,4] <= (100)),]
xmin_20_xmax_200 <- x[which(x[,4] >= 20 & x[,4] <= (200)),]
xmin_20_xmax_300 <- x[which(x[,4] >= 20 & x[,4] <= (300)),]
xmin_20_xmax_400 <- x[which(x[,4] >= 20 & x[,4] <= (400)),]
xmin_20_xmax_500 <- x[which(x[,4] >= 20 & x[,4] <= (500)),]
xmin_20_xmax_600 <- x[which(x[,4] >= 20 & x[,4] <= (600)),]
xmin_20_xmax_700 <- x[which(x[,4] >= 20 & x[,4] <= (700)),]
xmin_20_xmax_800 <- x[which(x[,4] >= 20 & x[,4] <= (800)),]
xmin_20_xmax_900 <- x[which(x[,4] >= 20 & x[,4] <= (900)),]
xmin_20_xmax_1000 <- x[which(x[,4] >= 20 & x[,4] <= (1000)),]
#until xmin_30 and max_1000
# xmin <- steps by 5
# xmax <- steps by 100
如果您需要更多信息,请告诉我,我会提供。
我认为这会满足您的需求,是吗?
mins <- c(15, 20); maxs <- seq(100, 1000, by = 100)
combs <- expand.grid(mins,maxs)
for(i in 1:nrow(combs)){
x_iter <- x[x[,4] >= combs[i,1] & x[,4] <= combs[i,2], ]
# do here whatever you want to do on x_iter
}
另一种方法:向量化来自 dplyr 或 data.table 包的 between
函数:
set.seed(1)
x <- sample(1:1000, 100)
vbetween <- Vectorize(dplyr::between, vectorize.args = c("left", "right"), SIMPLIFY = FALSE)
lapply(vbetween(x, rep(10, 2), c(100, 200)), which)
# [[1]]
# [1] 10 27 47 55 56 69 92
#
# [[2]]
# [1] 10 12 24 27 34 38 47 55 56 69 86 88 90 92
我想用以下代码在 R 中创建一个 for 循环。我尝试了很多方法,但没有用。有人知道如何在没有重复代码的情况下为此创建一个 for 循环吗?抱歉,如果这是一个非常愚蠢的问题。
#x[,4]
#[1] 234 788 211 350 532 98 77 45 123 900 135 841 283 15 421 19 643 934
以上是我要筛选的列的一部分。 我想用下面的代码过滤它。
xmin_15_xmax_100 <- x[which(x[,4] >= 15 & x[,4] <= (100)),]
xmin_15_xmax_200 <- x[which(x[,4] >= 15 & x[,4] <= (200)),]
xmin_15_xmax_300 <- x[which(x[,4] >= 15 & x[,4] <= (300)),]
xmin_15_xmax_400 <- x[which(x[,4] >= 15 & x[,4] <= (400)),]
xmin_15_xmax_500 <- x[which(x[,4] >= 15 & x[,4] <= (500)),]
xmin_15_xmax_600 <- x[which(x[,4] >= 15 & x[,4] <= (600)),]
xmin_15_xmax_700 <- x[which(x[,4] >= 15 & x[,4] <= (700)),]
xmin_15_xmax_800 <- x[which(x[,4] >= 15 & x[,4] <= (800)),]
xmin_15_xmax_900 <- x[which(x[,4] >= 15 & x[,4] <= (900)),]
xmin_15_xmax_1000 <- x[which(x[,4] >= 15 & x[,4] <= (1000)),]
xmin_20_xmax_100 <- x[which(x[,4] >= 20 & x[,4] <= (100)),]
xmin_20_xmax_200 <- x[which(x[,4] >= 20 & x[,4] <= (200)),]
xmin_20_xmax_300 <- x[which(x[,4] >= 20 & x[,4] <= (300)),]
xmin_20_xmax_400 <- x[which(x[,4] >= 20 & x[,4] <= (400)),]
xmin_20_xmax_500 <- x[which(x[,4] >= 20 & x[,4] <= (500)),]
xmin_20_xmax_600 <- x[which(x[,4] >= 20 & x[,4] <= (600)),]
xmin_20_xmax_700 <- x[which(x[,4] >= 20 & x[,4] <= (700)),]
xmin_20_xmax_800 <- x[which(x[,4] >= 20 & x[,4] <= (800)),]
xmin_20_xmax_900 <- x[which(x[,4] >= 20 & x[,4] <= (900)),]
xmin_20_xmax_1000 <- x[which(x[,4] >= 20 & x[,4] <= (1000)),]
#until xmin_30 and max_1000
# xmin <- steps by 5
# xmax <- steps by 100
如果您需要更多信息,请告诉我,我会提供。
我认为这会满足您的需求,是吗?
mins <- c(15, 20); maxs <- seq(100, 1000, by = 100)
combs <- expand.grid(mins,maxs)
for(i in 1:nrow(combs)){
x_iter <- x[x[,4] >= combs[i,1] & x[,4] <= combs[i,2], ]
# do here whatever you want to do on x_iter
}
另一种方法:向量化来自 dplyr 或 data.table 包的 between
函数:
set.seed(1)
x <- sample(1:1000, 100)
vbetween <- Vectorize(dplyr::between, vectorize.args = c("left", "right"), SIMPLIFY = FALSE)
lapply(vbetween(x, rep(10, 2), c(100, 200)), which)
# [[1]]
# [1] 10 27 47 55 56 69 92
#
# [[2]]
# [1] 10 12 24 27 34 38 47 55 56 69 86 88 90 92