2

I searched a lot to find similar post to my post below but no luck yet I have 1 column of data like below (extracted from original big file having many columns)

C1 
0 
1 
2 
3 
4 
3
3
2
1

From this data I want to generate a new column C2 where in C2 should just indicate where my C1 column values are above and below a threshold compared to max value. In this case max(C1) is 4. So If set threshold of 2 then the new data should be like below.

C1 C2 
0  0
1  0
2  1
3  1
4  1
3  1
3  1
2  1 
1  0

Note: My data always have a increasing trend upto some point and then decreasing trend after that. I know how to do simple plain subset on a particular column but I am not getting the logic to subset when there is a increasing and decreasing trend.

Thanks in advance.

1 Answers1

0

I would use the plyr package in r, and use an ifelse statement as a part of the mutate function. I will write my code and then explain. I assume you already have the C1 vector in a data frame named df

install.packages('plyr')

library(plyr)

df2 <- mutate(df, c2 = ifelse(c1 >= 2,1,0))

The mutate function creates a new column that satisfies whatever function you wish. In this case I used the ifelse function similar to excel's IF() function that inputs:

Condition , What happens if True , What happens if false.

Hope that helps =)

Community
  • 1
  • 1
Emby
  • 1