Tag: R

R cheat sheet

Replace NA in data.frame with 0

df[is.na(df)] = 0

Sum values in data.frame by some ID

df = data.frame(id = c(‘a’, ‘a’, ‘b’, ‘b’), value = c(1,2,3,4))

aggregate(formula = value ~ id, data = df , FUN = sum)

  id value
1  a     3
2  b     7

Ignore NA

length(na.omit(x))

Only retain rows that match a values in some column

df = data.frame(id = c(‘a’, ‘a’, ‘b’, ‘b’), value = c(1,2,3,4))

selected_ids = c(‘a’)

df[df$id %in% selected_ids,]