Lime
Lime

Reputation: 754

how to fill missing values as zero

I have data that denotes 'X' as a missing value, and I want to fill these with zeros, then assort the data by bcr_code by selecting only bcr 27, 28, and 29.

I have tried

ebird %>% group_by(protocol_type) %>% select(common_name, observation_count, observation_date, bcr_code)

to get it into the data.frame format, but I want to group them by specific values onto a new data.frame, by only selecting a few values.

Reproducible code:

structure(list(protocol_type = c("Traveling", "Traveling", "Incidental", 
"Incidental", "Incidental", "Incidental"), common_name = c("Bachman's Sparrow", 
"Bachman's Sparrow", "Bachman's Sparrow", "Bachman's Sparrow", 
"Bachman's Sparrow", "Bachman's Sparrow"), observation_count = c("1", 
"1", "X", "2", "2", "X"), observation_date = structure(c(18004, 
18363, 11796, 13331, 13636, 13698), class = "Date"), bcr_code = c(27L, 
27L, 27L, 27L, 27L, 27L)), row.names = c(NA, -6L), groups = structure(list(
    protocol_type = c("Incidental", "Traveling"), .rows = structure(list(
        3:6, 1:2), ptype = integer(0), class = c("vctrs_list_of", 
    "vctrs_vctr", "list"))), row.names = 1:2, class = c("tbl_df", 
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df", 
"tbl_df", "tbl", "data.frame"))

Upvotes: 0

Views: 98

Answers (2)

fabla
fabla

Reputation: 1816

In Base R you could:

# 0 instead of X

ebird[ebird$observation_count == "X", 3] <- 0

# only keep 27 | 28 | 29 

ebird <- ebird[ebird$bcr_code %in% c(27, 28, 29), ]

# which in this case keeps all given observations

Upvotes: 2

Edo
Edo

Reputation: 7818

Equivalent in dplyr

library(dplyr)

ebird %>%
  mutate(observation_count = as.numeric(replace(observation_count, observation_count == "X", 0))) %>% 
  filter(bcr_code %in% c(27, 28, 29))

Upvotes: 3

Related Questions