Thiyagu
Thiyagu

Reputation: 111

To find the difference between two column elements in a data frame

My question is a very basic one and I am new to R and programming. I have a data frame RESULT with two columns Max, Min and 500 rows. I just want to find the difference between Max and Min and put the value in a third column Difference. I tried with the code:

select Max, Min, Max-Min as Difference from RESULT.

But I am getting

Error: unexpected symbol in "select Max".

I also tried just with:

Difference<-c(RESULT$Max-RESULT$Min) 

for which I am getting:

Warning Message

In Ops.factor(RESULT$Max, RESULT$Min) : - not meaningful for factors

RESULT:

    Max Min
1   NaN NaN
2   25  NaN
3   NaN NaN
4   NaN NaN
5   NaN NaN
6   25.6   23.1

I want to display NaN or any other variable like X or Y in Difference column whenever there is NaN in Either of Max or Min. The output should be like:

    Max Min   Difference
1   NaN NaN   NaN
2   25  NaN   NaN
3   NaN NaN   NaN
4   NaN 34    NaN
5   NaN NaN   NaN
6   25.6   23.1   2.5

Upvotes: 9

Views: 67850

Answers (3)

Aman Burman
Aman Burman

Reputation: 299

You can also try sqldf package which perform SQL Selects on R data frames.

RESULT <- sqldf("select MAX, MIN, (MAX - MIN) AS Difference from RESULT")

In this way you will have 3 columns MAX, MIN & RESULT.

Upvotes: 1

Vishnu Kumar
Vishnu Kumar

Reputation: 41

You can use below code to produce the desire output:

# Please import data as df.csv with given 2 columns name and respective data
setwd("D:\\Vishnu Jayswal\\Data_Science")
read.csv("df.csv")
View(df)
di <- ifelse(is.nan(df$Max) ==TRUE | is.nan(df$Min) ==TRUE,NaN,df$Max-df$Min)
di
df_final <- data.frame(df,di)
df_final

Upvotes: 2

TheComeOnMan
TheComeOnMan

Reputation: 12905

If df is your data.frame, df$V3 <- df$V1 - df$V2 should add a new column called V3 which is the difference of columns V1 and V2.

Your error message says that the columns are factors. You can convert them to a numeric class by doing df$V1 <- as.numeric(as.character(df$V1)) and similarly for V2

Upvotes: 16

Related Questions