-1

I am pretty new with R (R studio user), but more and more enthousiastic.

I imported an Excel file in R (R studio). No problem. Big file with 29 columns and 14.000 rows. One column contains only two possible values: S or E.

What I did (after trying a lot of other coding) is: I took the column out of the dataset:

kolom <- data1$spoedelectief
# this worked wel. Pretty long view...:
> View(kolom)
#then I wrote this. Worked good as well. 
> length(which(kolom == "S"))
[1] 1999
#and the same code counted the amount of E as well.
> length(which(kolom == "E"))
[1] 11322

I found examples that show how to count NA in a column / dataset. Found some other coding, but nothing seems to work (or is it me...). My question: the code as shown works. But I guess there is a method to count directly in my dataframe of 29 colums and all those rows.

dataframe name is data1. Column: spoedelectief. What code can I use to directly count the E or S on that column?

camille
  • 16,432
  • 18
  • 38
  • 60
Janneman
  • 343
  • 3
  • 13

1 Answers1

0

We can use sum directly

sum(kolom == "E", na.rm = TRUE)

Or if there are only two unique elements in that column, table should work as well in getting the frequency count of both elements

table(kolom)

To apply this repeatedly, can create a function

f1 <- function(vec, value){
        sum(vec == value, na.rm = TRUE)
  }
f1(kolom, "E")
f1(kolom, "S")
akrun
  • 874,273
  • 37
  • 540
  • 662