-1

I have a dataframe with these columns:

   criticidad.x     criticidad.y
1            NA               60
2            NA               10
3            40               NA
4            40               NA
5            NA               NA
6            NA               10

How could I combine it so I get the following:

   criticidad.x     criticidad.y     criticidad
1            NA               60             60
2            NA               10             10
3            40               NA             40
4            40               NA             40
5            NA               NA             NA
6            NA               10             10

The dataframe has other columns that I'd like to keep untouched.

Thanks!

MustardRecord
  • 305
  • 4
  • 14

1 Answers1

1

A quick and dirty solution might be:

First, ignore the NA values and do a simple row sum:

df$criticidad <- rowSums(df[,c("criticidad.x","criticidad.y")], na.rm = T)

Then correct for when both columns had NA values:

df$c[is.na(df$x) & is.na(df$y)] <- NA

There are more answers here: Sum of two Columns of Data Frame with NA Values

Oxi4
  • 51
  • 1