多个条件,然后创建新列

问题描述 投票:-2回答:1

我有一个包含两列的数据集,我需要创建第三列,其中第一列和第二列都带有条件。

set.seed(1)
x1=(sample(1:10, 100,replace=T))
y1=sample(seq(1,10,0.1),100,replace=T)

z=cbind(x1,y1)
unique(as.data.frame(z)$x1)
z%>%as.data.frame()%>%dplyr::filter(x1==3)

table(x1)
 1  2  3  4  5  6  7  8  9 10 
 7  6 11 14 14  5 11 15 11  6 

> z%>%as.data.frame()%>%dplyr::filter(x1==3)
   x1   y1
1   3  6.9
2   3  9.5
3   3 10.0
4   3  5.6
5   3  4.1
6   3  2.5
7   3  5.3
8   3  9.5
9   3  5.5
10  3  8.9
11  3  1.2

例如,当我过滤x == 3时,可以看到y1值,我需要在第11行写1,其余将为0。我需要在该列中找到最小值。我的原始数据集有43545行,但只有638个唯一数字(如x1)。表x1显示1重复7次,但是在我的数据集中有些频率为1,有些频率为100。我应该使用case_when,但是如何检查每个y1来找到最小的1。

r dplyr subset selection mutate
1个回答
0
投票

如果我理解正确,您正在寻找x1的每个值的y1值最小的行

library(tidyverse)
z %>% as.data.frame() %>% 
      group_by(x1) %>% 
      arrange(y1) %>% # sort values by increasing order within each group
      mutate(flag = ifelse(row_number()==1,1,0)) %>% # create flag for first row in group
      ungroup()
© www.soinside.com 2019 - 2024. All rights reserved.