r - Dichotomize data by factor -
i need create dichtomized variable based on 2 factors (one hopes it's possible).
let's have data:
d <- data.frame ( agegroup = c(2,1,1,2,3,2,1,3,3,3,3,3,1,1,2,3,2,1,1,2,1,2,2,3) , gender = c(2,2,2,2,2,2,1,2,1,1,1,2,1,1,2,2,1,1,1,1,2,1,1,1) , hourwalking = c(0.3,0.5,1.1,1.1,1.1,1.2,1.2,1.2,1.3,1.5,1.7,1.8,2.1,2.1,2.2,2.2,2.3,2.4,2.4,3,3.1,3.1,4.3,5) )
i create binary (lowwalkhrs) using gender- , agegroup-specific median (e.g., when agegroup = 1 , gender = 1, median = 2.1 (median found using excel)). lowwalkhrs added variable in dataset, output be:
agegroup gender hourwalk lowwalkhrs 2 2 0.3 1 1 2 0.5 1 1 2 1.1 0 2 2 1.1 1 3 2 1.1 1 2 2 1.2 0 1 1 1.2 1 .... 3 1 5 0
i have rather large dataset (~10k observations), excel out of question.
in r i've tried cut , cut2, doesn't seem take factor variables, ddply, gave me error message of (error in $<-.data.frame
(*tmp*
, "lowwalkhrs", value = list(hourwalking = c(0.63, : replacement has 949 rows, data has 11303).
i suspect might slow, think works:
z <- mapply(d$agegroup, d$gender, d$hourwalking, fun=function(a,g,h) as.numeric(h < median(d$hourwalking[d$agegroup==a & d$gender==g])) )
Comments
Post a Comment