我希望r中的功能检查一个列的值是否大于第75分位数,然后在下一个列中写入是或否

发布于 2025-01-21 12:24:32 字数 309 浏览 2 评论 0原文

我已经尝试了以下公式,但是即使我更改分数值,也可以提供所有NOS。 注意:我有3个独立数据集,我想应用该功能。

outlier<-function(x1,x2){
  q1<-quantile(x1 , .75, na.rm = TRUE)
    if(x1>q1){x2<-"Yes"
    }else{
      x2<-"No"
    
  }

}

我尝试了x2&lt; -ifelse(x1&gt; q1,“ yes”,“ no”) 在功能内部,但仍然不起作用。

I have tried the following formula but it gives all nos even when I change the quantile value.
NOTE: I have 3 independent datasets that I want to apply the function.

outlier<-function(x1,x2){
  q1<-quantile(x1 , .75, na.rm = TRUE)
    if(x1>q1){x2<-"Yes"
    }else{
      x2<-"No"
    
  }

}

I have tried x2<-ifelse(x1>q1,"Yes","No")
inside the function but it still doesn't work.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

空袭的梦i 2025-01-28 12:24:32

您可以使用ifelse语句,并使用Mutate创建一个新列。

library(dplyr)
set.seed(1)

df <- tibble(x1 = sample(c(1:10), size = 10, replace = T))

df %>% 
  mutate(x2 = ifelse(quantile(x1, 0.75, na.rm = T) < x1, "Yes", "No"))

如果您想要功能

library(dplyr)
set.seed(1)

df <- tibble(x1 = sample(c(1:10), size = 10, replace = T), 
             x2 = sample(c(1:10), size = 10, replace = T),
             x3 = sample(c(1:10), size = 10, replace = T),
             x4 = sample(c(1:10), size = 10, replace = T))




outlier<-function(dataframe, quant = 0.75, col = c("x1", "x2")){
  
  dataframe %>% 
    mutate(across(all_of(col), ~ifelse(.x>quantile(.x,0.75), 'Yes', 'No'),
                  .names = '{col}_yes'))

  }

outlier(dataframe = df,quant =  0.25)

You can use an ifelse statement and create a new column using mutate.

library(dplyr)
set.seed(1)

df <- tibble(x1 = sample(c(1:10), size = 10, replace = T))

df %>% 
  mutate(x2 = ifelse(quantile(x1, 0.75, na.rm = T) < x1, "Yes", "No"))

If you want a function

library(dplyr)
set.seed(1)

df <- tibble(x1 = sample(c(1:10), size = 10, replace = T), 
             x2 = sample(c(1:10), size = 10, replace = T),
             x3 = sample(c(1:10), size = 10, replace = T),
             x4 = sample(c(1:10), size = 10, replace = T))




outlier<-function(dataframe, quant = 0.75, col = c("x1", "x2")){
  
  dataframe %>% 
    mutate(across(all_of(col), ~ifelse(.x>quantile(.x,0.75), 'Yes', 'No'),
                  .names = '{col}_yes'))

  }

outlier(dataframe = df,quant =  0.25)
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文