Home > database >  How to validate whether a word in a dataframe is a name in R
How to validate whether a word in a dataframe is a name in R

Time:12-06

I have an R script that filters data to a new data frame. Between other things it filters the most repeated word as follows:

Word Times
oliver 3
great 8
jacob 2
fantastic 6

Is there a way in R that given a list of names, names <- c('oliver','jacob','harry', 'jack'), I can filter the last data frame to something like this?

(That takes the names, count them, and then add them to a new row named names that counts the times all the names appeared)

Word Times
names 5
great 8
fantastic 6

CodePudding user response:

I use x instead of names.

Base R way

x <-  c('oliver','jacob','harry', 'jack')

y <- sum(df$Times[df$Word %in% x])
rbind(c("names", y), df[!(df$Word %in% x), ])

       Word Times
1     names     5
2     great     8
4 fantastic     6

CodePudding user response:

A dplyr solution

names <- c('oliver','jacob','harry', 'jack')

df %>% 
     summarize_each(funs(ifelse(Word %in% names, "names", .))) %>%
     group_by(Word) %>% 
     summarize(sum(Times))

# A tibble: 3 × 2
  Word      `sum(Times)`
  <chr>            <dbl>
1 fantastic            6
2 great                8
3 names                5
  • Related