简体   繁体   中英

COUNTIF function in R to a new column

I am using a data set of public transit information in rstudio. One column in this huge data frame is Origin Station. I'd like to be able to count the number of times each specific station appears as an origin station and then create a new column with that value. I'd do this in excel but the data file is way too big. IE, for every record where "14 Street-Union Sq" is the value for Origin Station, there will be a new column counting the total number of times that 14 St-Union Sq was the Origin Station.

Thanks.

sounds like the dplyr package and the n() function along with a group_by variable. Try something like this:

df <- data.frame(origin = sample(letters[1:5], 1000, replace = TRUE),
             other_column = rnorm(1000))

library(dplyr)

df %>% group_by(origin) %>% mutate(n_appearances = n())

您可以使用ave函数

test['count']=with(test,ave(variable, variable, FUN=function(x) length(x)))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM