Minfetli
Minfetli

Reputation: 299

Fill in empty values in column of dataframe by condition

I have the followin dataframe. Now I want to fill in the empty values in "product" by determining the value of the code 44 and 90. 44 should be "shirt" and 90 "sweater".

What's the best way to do this? With a for loop?

data = data.frame("code" = c(44,78,21,90,100,44,90), "product" = c("","hat","shoe","","umbrella","",""))
> data
  code  product
1   44         
2   78      hat
3   21     shoe
4   90         
5  100 umbrella
6   44         
7   90      

Upvotes: 1

Views: 987

Answers (2)

GKi
GKi

Reputation: 39647

You can use match and use the indices for subsetting.

i <- match(data$code, c(44, 90))
j <- !is.na(i)
data$product[j] <- c("shirt", "sweater")[i[j]]
data
#  code  product
#1   44    shirt
#2   78      hat
#3   21     shoe
#4   90  sweater
#5  100 umbrella
#6   44    shirt
#7   90  sweater

Upvotes: 1

Greg
Greg

Reputation: 3650

Using dplyr first convert the product variable to character (from factor), then use case_when

library(dplyr)
data %>% 
  mutate_if(is.factor, as.character) %>% 
  mutate(product = case_when(product == "" & code == 44 ~ "shirt",
                             product == "" & code == 90 ~ "sweater",
                             TRUE ~ product))

  code  product
1   44    shirt
2   78      hat
3   21     shoe
4   90  sweater
5  100 umbrella
6   44    shirt
7   90  sweater

Using base, same idea - first convert factors to character than then use ifelse

i <- sapply(data, is.factor)
data[i] <- lapply(data[i], as.character)

data$product[data$product == ""] <- ifelse(data$code[data$product == ""] == 44, "shirt", "sweater")
data
  code  product
1   44    shirt
2   78      hat
3   21     shoe
4   90  sweater
5  100 umbrella
6   44    shirt
7   90  sweater

Also worth noting, if you use data.frame with stringsAsFactors = FALSE all the factor converting becomes unnecessary.

Upvotes: 2

Related Questions