-3

i have a data.frame with 10000 obs and 14 variables in R. i wanna to write a command same as count ifs formula in excel.

  • would this work for you? `sum(ifelse(test = ..., yes = 1, no = 0))` – Hutch3232 Jun 19 '22 at 13:25
  • my dataframe name is "df" – aboozar varzi Jun 19 '22 at 13:39
  • could you write a commend by this name? i mean "df" – aboozar varzi Jun 19 '22 at 13:39
  • suppose that my dataframe has som character variable. – aboozar varzi Jun 19 '22 at 13:42
  • 2
    Hi @aboozarvarzi. Please review how [How do I ask a good question?](https://stackoverflow.com/help/how-to-ask) and also [how to make a good reprex](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Your question is about to get closed for not meeting SO's posting standards. In short: Provide reproducible & minimal sample data, a clear & complete problem statement (main post, not in comments), and your expected output. – Maurits Evers Jun 19 '22 at 14:06

1 Answers1

0

It might help to provide some example data in your question. Here are a couple examples based on the built-in iris data:

df <- iris

# count how many records have species virginica
sum(ifelse(df$Species == "virginica", 1, 0))
# [1] 50

# count how many records have Petal.Length < 1.5
sum(ifelse(df$Petal.Length < 1.5, 1, 0))
# [1] 24

# count how many records have species setosa and Petal.Length > 1.5
sum(ifelse(df$Species == "setosa" & df$Petal.Length > 1.5, 1, 0))
# [1] 13

# count how many records have species setosa or virginica
sum(ifelse(df$Species == "setosa" | df$Species == "virginica", 1, 0))
# [1] 100

# same as above, different syntax
sum(ifelse(df$Species %in% c("setosa", "virginica"), 1, 0))
# [1] 100
Hutch3232
  • 408
  • 4
  • 11
  • 4
    None of these `ifelse` statements are necessary, as the conditional statements already return TRUE & FALSE. For example, the first example should be `sum(iris$Species == "virginica")`. That side, I'd be better to have OP improve his post first and clarify on what it is he/she is after. Stack Overflow helps with *specific* coding questions. General tutorial-like help is not a good fit for SO. This is not to discredit your contribution but to encourage OP to spend time familiarising him/herself with basic R syntax. – Maurits Evers Jun 19 '22 at 14:17
  • Great point, removing `ifelse` is simpler and faster, thank you! Also good point - I shouldn't have to guess what OP wants. Thanks! – Hutch3232 Jun 19 '22 at 14:23
  • i give my response. thank you so much!!!! – aboozar varzi Jun 19 '22 at 14:27