Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

r

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
like image 375
Thiyagu Avatar asked Nov 07 '13 14:11

Thiyagu


1 Answers

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

like image 168
TheComeOnMan Avatar answered Oct 21 '22 11:10

TheComeOnMan