我知道标题可能不是很明确,抱歉,我找不到更好的标题。
我试图做以下事情
foo<-123456 # what this is doesn't really matter, just know it's a value
ifelse(my_df$number / foo < 0.75, #for each row of my_df, see if number / sum is under 0.75
ifelse( # if it is...
my_df$number / foo < 0.5, # check if it is under 0.5
my_df$class<-"in50", # if it is, assign the value "in50" in the "class" field of this row
my_df$class<-"in75"), # else, assign "in75" to the class field of this row
my_df$class<-"in100") # if it's not under 0.75, assign "in100" to the class field of this row
我的问题在于分配运算符:
如果我使用<-
,则整个class
列最终为in100
(因为最后一个值应为in100
)。
如果我使用=
,我明白了:
错误:意外&#39; =&#39; in:&#34; my_df $ number / sum&lt; 0.5,my_df $ class =&#34;
显然,==
不是我需要的。
有什么建议吗?
编辑:我评论了代码以使预期结果更清晰答案 0 :(得分:2)
我们可以使用findInterval
i1 <- with(my_df, findInterval(number/foo, c(0.5, 0.75)))
my_df$class <- c('in50', 'in75', 'in100')[i1+1L]
head(my_df$class)
#[1] "in50" "in50" "in100" "in100" "in100" "in100"
head(my_df$number/foo)
#[1] 0.472 0.360 1.128 0.832 1.064 1.480
foo <- 125
set.seed(24)
my_df <- data.frame(number= sample(1:200,
100, replace=TRUE))