Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to safely replace values in data.frame even when data.frame is empty?

Tags:

dataframe

r

I'm having trouble writing safe code to manipulate data frames in R. The problem I'm having is that df[...] <- (...) is vulnerable to the data.frame having no rows (among other things!).

Example 1:

df <- data.frame(a = 1:2, b = c(NA, 5))
df[is.na(df$b), 'b'] <- 0

Does what I want - replaces NA with 0.

Example 2:

df.empty <- data.frame(a = character(), b = character())
df.empty[is.na(df.empty$b), 'b'] <- 0
df.empty[is.na(df.empty$b), 'b', drop = F] <- 0

Both attempts to mutate the data frame in Example 2 result in an error (either replacement has 1 row, data has 0; or 'unused argument (drop = F)).

I find that very annoying.

How do I universally mutate a column based on arbitrary (vector) criterion in a data.frame, without having to manually check for all possible conditions like NA, NULL, no rows, coercion to vector because of no rows, etc.?

like image 681
Jakub P. Avatar asked May 15 '26 15:05

Jakub P.


1 Answers

Perhaps using dplyr?

This example, combining mutate() and ifelse(), seems to achieve what you want.

df.empty <- df.empty %>% 
    mutate(b = ifelse(is.na(b), 0, b))
like image 100
elikesprogramming Avatar answered May 18 '26 18:05

elikesprogramming



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!