过滤R数据帧中的特定列

时间:2015-10-19 07:11:13

标签: r statistics

我想用以下代码在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

如果您需要更多信息,请告诉我,我会提供。

2 个答案:

答案 0 :(得分:0)

我认为这会做你想做的事,是吗?

        private RelayCommand<string> _buttonClickCommand;
        public RelayCommand<string> ButtonClickCommand
        {
            get
            {
                return this._buttonClickCommand = new RelayCommand<string>((value) => this.ClickButton(value));
            }
        }

        private void ClickButton(string text)
        {
            if (text.Equals("Space"))
            {
            }
            else
            {
                this.Text += text;
            }
        }

答案 1 :(得分:0)

另一种方法:从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