Wednesday, 15 September 2010

r - create new variables using for loop and if condition -



r - create new variables using for loop and if condition -

i have info frame below

head(sip1) id interviewdate ss1 ss2 ss2b ss3 ss4_1 ss4_2 ss4_3 ss4_4 ss4_5 ss4_6 ss4_7 ss4_8 ss4_9 ss4_10 ss4_11 ss4_12 ss4_13 ss4_14 ss4_15 ss4_16 ss4_17 ss4_18 ss4_19 ss4_20 ss4_21 ss4_22 1 1 2014-03-19 4 1 1 2 1 1 4 1 1 1 1 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 2 2 2014-03-19 2 1 5 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 4 1 1 1 1 1 1 1 3 3 2014-03-19 3 1 8 2 1 1 1 4 1 1 4 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 4 4 2014-03-18 3 1 5 1 1 1 1 1 1 1 1 1 3 1 1 1 1 1 1 1 1 1 1 1 1 1 5 5 2014-03-18 3 1 8 1 1 1 1 1 1 1 1 1 3 1 1 1 1 2 1 1 1 1 1 1 1 1 6 6 2014-03-19 3 1 1 2 1 1 1 1 1 1 1 1 3 1 1 3 1 3 1 1 1 1 1 1 1 1 ss4_23 ss4_24 ss4_25 ss4_26 ss4_27 ss4_28 ss4_29 ss4_30 ss5 ss6 ss7 hs1_c1_a hs1_c1_b hs1_c1_c hs1_c1_d hs1c1 1 1 1 1 1 1 1 1 1 1 2 2 1 2 2 2 1 2 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 0 3 1 1 1 1 1 1 1 1 1 2 2 1 2 2 2 1 4 1 1 1 1 1 1 1 1 2 2 2 1 2 2 2 1 5 1 1 1 1 1 1 1 1 1 2 2 2 2 2 2 0 6 1 1 1 1 1 1 1 1 1 2 2 1 2 2 2 1

here want create series of new variables (eg. newss4_1newss4_2newss4_3...newss4_30)regarding ss4_1...ss4_30, using status newss4_1 [ss4_1==1] <- 0; newss4_1 [ss4_1!=1] <- 1. want create 30 variables binomial next condition: if old value=1, new value=0; if old value!=1, new value=1. , create them new variables.

could please help? have no thought how in loop write 30 new variables, , how utilize if conditions?

thanks,qiqi

using jazzurro's data

(foo[paste0("ss4_", 1:2)] !=1) + 0 # ss4_1 ss4_2 #[1,] 0 1 #[2,] 1 0 #[3,] 1 1 #[4,] 0 1 #[5,] 1 1

foo[paste0("ss4_", 1:2)] !=1 tests values not equal 1 - results in true or false. 1 way convert ones , zeros adding on zero.

edit: add together existing dataset

newvars <- (foo[paste0("ss4_", 1:2)] !=1) + 0 colnames(newvars) <- paste0("new", colnames(newvars)) cbind(foo, newvars)

edit2: using bigger dataset sip1, create index columns starting ss4_. can grep

indx <- grep("ss4_", colnames(sip1)) res <- cbind(sip1, setnames( as.data.frame((sip1[indx]!=1)+0), paste0("new", colnames(sip1[indx])))) data sip1 <- structure(list(id = 1:6, interviewdate = c("2014-03-19", "2014-03-19", "2014-03-19", "2014-03-18", "2014-03-18", "2014-03-19"), ss1 = c(4l, 2l, 3l, 3l, 3l, 3l), ss2 = c(1l, 1l, 1l, 1l, 1l, 1l), ss2b = c(1l, 5l, 8l, 5l, 8l, 1l), ss3 = c(2l, 1l, 2l, 1l, 1l, 2l), ss4_1 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_2 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_3 = c(4l, 1l, 1l, 1l, 1l, 1l), ss4_4 = c(1l, 1l, 4l, 1l, 1l, 1l), ss4_5 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_6 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_7 = c(1l, 2l, 4l, 1l, 1l, 1l), ss4_8 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_9 = c(1l, 1l, 1l, 3l, 3l, 3l), ss4_10 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_11 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_12 = c(1l, 1l, 1l, 1l, 1l, 3l), ss4_13 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_14 = c(1l, 1l, 1l, 1l, 2l, 3l), ss4_15 = c(1l, 4l, 1l, 1l, 1l, 1l), ss4_16 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_17 = c(3l, 1l, 1l, 1l, 1l, 1l), ss4_18 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_19 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_20 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_21 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_22 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_23 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_24 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_25 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_26 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_27 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_28 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_29 = c(1l, 1l, 1l, 1l, 1l, 1l), ss4_30 = c(1l, 1l, 1l, 1l, 1l, 1l), ss5 = c(1l, 1l, 1l, 2l, 1l, 1l), ss6 = c(2l, 2l, 2l, 2l, 2l, 2l), ss7 = c(2l, 2l, 2l, 2l, 2l, 2l), hs1_c1_a = c(1l, 2l, 1l, 1l, 2l, 1l), hs1_c1_b = c(2l, 2l, 2l, 2l, 2l, 2l), hs1_c1_c = c(2l, 2l, 2l, 2l, 2l, 2l), hs1_c1_d = c(2l, 2l, 2l, 2l, 2l, 2l), hs1c1 = c(1l, 0l, 1l, 1l, 0l, 1l)), .names = c("id", "interviewdate", "ss1", "ss2", "ss2b", "ss3", "ss4_1", "ss4_2", "ss4_3", "ss4_4", "ss4_5", "ss4_6", "ss4_7", "ss4_8", "ss4_9", "ss4_10", "ss4_11", "ss4_12", "ss4_13", "ss4_14", "ss4_15", "ss4_16", "ss4_17", "ss4_18", "ss4_19", "ss4_20", "ss4_21", "ss4_22", "ss4_23", "ss4_24", "ss4_25", "ss4_26", "ss4_27", "ss4_28", "ss4_29", "ss4_30", "ss5", "ss6", "ss7", "hs1_c1_a", "hs1_c1_b", "hs1_c1_c", "hs1_c1_d", "hs1c1"), row.names = c("1", "2", "3", "4", "5", "6"), class = "data.frame")

r variables if-statement for-loop

No comments:

Post a Comment