1

My question concern programming in R. My data set looks like this: enter image description here

I'd like to fill empty (blanks not NA) rows from column id_code with appropriate values coming form column distribution.type.

My idea was used i.e mutate function mutate(df$distribution.type [ifelse(df$id_code == "", df$distribution.type, df$id_code)]) or something like this:

df$id_code[df$id_code == ""] <- df$distribution.type[df$id_code == ""]

and a plenty of another concepts, but they didn't work. May I ask for your help ?

2 Answers2

1

Here is an option using data.table. We convert the 'data.frame' to 'data.table' (setDT(df)), based on the condition in 'i' i.e. rows where 'id_code' is '' (blank), we assign (:=) the corresponding element of 'distribution.type. It is assigning in place, so would be fast.

library(data.table)
setDT(df)[id_code=='', id_code:= distribution.type]
akrun
  • 674,427
  • 24
  • 381
  • 486
0

Here is a toy dataset:

df = data.frame('id_code'=c('','2','3',NA,''),'distribution.type'=c('A','B',NA,'D','E'),stringsAsFactors=F)

Below is a subsetting solution (that specifically excludes rows with NAs). See Subsetting R data frame results in mysterious NA rows

df$id_code[!is.na(df$id_code) & df$id_code == ""] <- df$distribution.type[!is.na(df$id_code) & df$id_code == ""]

Below is a solution using dplyr. Maybe see https://stackoverflow.com/a/28592577/2176211

library(dplyr)
df <- mutate(df,id_code = ifelse(id_code == "", distribution.type, id_code))
Community
  • 1
  • 1
user20061
  • 314
  • 3
  • 10