R: create new variable, the new value are not correct -
R: create new variable, the new value are not correct -
i creating new variable "hs" summarizing 4 variables "hs_a/b/c/d" in "data1" using:
hs <- rep(5,length(data1)) attach(data1) hs[ hs_a == 2 & hs_b == 2 & hs_c==2 & hs_d==2 ] <- 0 hs[ hs_a == 1 & hs_b == 2 & hs_c==2 & hs_d==2 ] <- 1 hs[ hs_b == 1 & hs_a == 2 & hs_c==2 & hs_d==2 ] <- 2 hs[ hs_c == 1 & hs_a == 2 & hs_b==2 & hs_d==2 ] <- 3 hs[ hs_d == 1 & hs_a == 2 & hs_b==2 & hs_c==2 ] <- 4 hs1<-data.frame(hs_a,hs_b,hs_c,hs_d,hs)
i want other values in hs
5
, if not 0/1/2/3/4, utilize rep
5. problem that, values in hs
na
, whereas think should 5
.
there no na
in data1
, , have checked rows hs
of na
using (which(!!rowsums(is.na(hs)))
). #[1] 1545 1646 1687 1744 1784 1817
, "1212na
" "1112na
" "1112na
" "1112na
""1112na
""1112na
".
can help explain why? , there way solve or simpler way create variable?
also how new variable in for
loop?
i new r. thanks, qiqi
here assuming there na
values in original dataset data1
. so, if row contains na
value, of conditions specified become false
, value of 5
. there might cases combinations between columns homecoming false conditions, given 5
.
indx <- as.character(interaction(data1, sep='')) lvls <- c('2222', '1222', '2122', '2212', '2221') indx[!indx %in% lvls] <- 6 data1$hs <- as.numeric(factor(indx, levels=c(lvls,6)))-1
here glimpse of data1
lapply(split(data1, data1$hs), head,2) #$`0` # hs_a hs_b hs_c hs_d hs #1029 2 2 2 2 0 #1064 2 2 2 2 0 #$`1` # hs_a hs_b hs_c hs_d hs #164 1 2 2 2 1 #661 1 2 2 2 1 #$`2` # hs_a hs_b hs_c hs_d hs #46 2 1 2 2 2 #211 2 1 2 2 2 #$`3` # hs_a hs_b hs_c hs_d hs #349 2 2 1 2 3 #429 2 2 1 2 3 #$`4` # hs_a hs_b hs_c hs_d hs #152 2 2 2 1 4 #307 2 2 2 1 4 #$`5` # hs_a hs_b hs_c hs_d hs #1 na 2 3 1 5 #2 1 3 na 2 5
data set.seed(345) data1 <- as.data.frame(matrix(sample(c(na, 1:3), 4*1500, replace=true), ncol=4, dimnames=list(null, paste("hs", letters[1:4], sep="_"))) )
r variables
Comments
Post a Comment