Im trying to write a function with nested if-else in R. How can I convert a data.frame where the values of columns are set to:
Input
df <- read.table(header = TRUE, text="Chr start end num seg.mean seg.mean.1 seg.mean.2
1 68580000 68640000 A8430 0.7000 0 0.1032
1 115900000 116260000 B8430 0.0039 2.7202 2.7202
1 173500000 173680000 C5 -1.7738 -2.0746 -0.2722")
condition:
x > 0 & x< 1 : 1
x >= 1 : 2
x < 0 & x > - 1 : -1
x <= -1 : -2
x = 0 : 0
expected output
df <- read.table(header = TRUE, text="Chr start end num seg.mean seg.mean.1 seg.mean.2
1 68580000 68640000 A8430 1 0 1
1 115900000 116260000 B8430 1 2 2
1 173500000 173680000 C5 -2 -2 -1")
fun_cond <- function(x) { ifelse( x >= 1, 2,ifelse( x > 0 & x < 1, 1),ifelse( x <= 1, 2,ifelse( x < 0 & x > -1, -1)))}
new_df[5:length(df)] <- lapply(new_df[5:length(df)], fun_cond)