R根据if else语句选择行

时间:2018-02-13 19:43:37

标签: r if-statement dataframe subset plyr

我在基于if else条件限制数据集时遇到了问题。

这是我的数据框的一个例子:

mydf<-data.frame(chemical=c("Cd","Cd","Cd","Cd","Pb","Pb"),species=c("a","a","a","a","b","d"),scores=c(0,1,2,3,0,0))

我需要选择:对于每种化学品和物种,如果scores>0选择得分最小的行,否则选择0

我可以做得最低分,但我似乎无法成功添加if else语句。

ddply(mydf,.(chemical,species),function(x) x[which.min(x$score),])

结束表应该是这样的:

chemical species scores
1       Cd       a      1
2       Pb       b      0
3       Pb       d      0

3 个答案:

答案 0 :(得分:3)

mydf %>% 
  group_by(chemical, species) %>% 
  summarize(scores = ifelse(any(scores > 0), min(scores[scores>0]), 0))

我不知道这是否更快,但只是为了好玩,你也可以这样做

mydf %>% 
  group_by(chemical, species) %>% 
  summarize(scores = min(max(scores, 0)))

答案 1 :(得分:0)

这里有可行的解决方案,使用OP的原始逻辑,可能不是最优雅的代码

<强> plyr

ddply(mydf,.(chemical,species),
           function(x) x[if(any(x$scores != 0)) {which.min(replace(x$scores, x$scores == 0, NA))} else which(x$scores == 0),])

<强> dplyr

mydf %>%
     group_by(chemical, species) %>%
     do(.[if(any(.$scores != 0)) {which.min(replace(.$scores, .$scores == 0, NA))} else which(.$scores == 0),])

解压缩ifelse逻辑

# If none of the values are equal to 0
if(any(.$scores != 0))
# Find the index of the smallest values from a vector where 0 has been replaced by NA
{which.min(replace(.$scores, .$scores == 0, NA))} 
# Else find index of value equal to 0
else which(.$scores == 0)

答案 2 :(得分:0)

这应该达到你想要的效果:

= TEXT(12345,"######000000") & "." & TEXT(12,"######000000") & "." & TEXT(0,"###000")