for loop - passing positive results from multiple columns into a single new column in r -
i trying work out way create single column multiple columns in r. want r go through rows multiple columns , if finds positive result in 1 of columns, pass result 'amalgam' column (sorry don't know better word it).
see toy dataset below
x <- c(na, na, na, na, na, 1) y <- c(na, na, 1, na, na, na) z <- c(na, 1, na, na, na, na) df <- data.frame(cbind(x, y, z)) df[, "compcol"] <- na df x y z compcol 1 na na na na 2 na na 1 na 3 na 1 na na 4 na na na na 5 na na na na 6 1 na na na
i need pass positive results each of columns compcol column while changing negative results 0. looks this.
x y z compcol 1 na na na 0 2 na na 1 3 3 na 1 na 2 4 na na na 0 5 na na na 0 6 1 na na 1
i know if requires if else statement nested inside loop ways have tried result in errors don't understand.
i tried following single column
(i in 1:length(x)) { if (df$x[i] == 1) { df$compcol[i] <- df$x[i] } }
but didn't work @ all.
i got message 'error in if (df$x[i] == 1) { : missing value true/false needed'
and makes sense can't see put true/false statement
we can use max.col
. create logical matrix checking whether selected columns greater 0 , not na ('ind'). use max.col
column index each row , multiply rowsums
of 'ind' if there 0 true values row, 0.
ind <- df > 0 & !is.na(df) df$compcol <- max.col(ind) *rowsums(ind) df$compcol #[1] 0 3 2 0 0 1
or option pmax
after multiplying col(df)
do.call(pmax,col(df)*replace(df, is.na(df), 0)) #[1] 0 3 2 0 0 1
note: used dataset before creating 'compcol' in op's post.
Comments
Post a Comment