Keyan H
Keyan H

Reputation: 37

finding the range between two columns in one dataframe R

I have a dataframe that looks like this:

Year   Month  Day   High   Low
1999    2      2      10     5
1999    2      3      11     4
1999    2      4      NA     NA
1999    2      5      11     5
....    ..    ..      ..     ..

I want to calculate the range (the difference) between High and Low for every row and assign it to a new column, with NA values not counted.

My desired output is:

    Year   Month  Day   High   Low   Range
    1999    2      2      10     5     5
    1999    2      3      11     4     7
    1999    2      5      11     5     6
    ....    ..    ..      ..     ..    ..

Upvotes: 0

Views: 336

Answers (2)

akrun
akrun

Reputation: 887301

We can use complete.cases in base R

transform(df[complete.cases(df),], Range = High - Low)

Or with data.table

library(data.table)
setDT(df)[complete.cases(.SD)][, Range := High - Low]

Or using dplyr

library(dplyr)
df %>%
     filter(across(c(High, Low), ~ !is.na(.))) %>%
     mutate(Range = High - Low)

Upvotes: 0

Ronak Shah
Ronak Shah

Reputation: 389055

You can use na.omit to remove NA values and add column with transform

transform(na.omit(df), Range = High - Low)

#  Year Month Day High Low Range
#1 1999     2   2   10   5     5
#2 1999     2   3   11   4     7
#4 1999     2   5   11   5     6

Or using dplyr :

library(dplyr)
df %>%
  filter(!is.na(High) | !is.na(Low)) %>%
   mutate(Range = High - Low)

Upvotes: 1

Related Questions