langensk@fas.harvard.edu
2003-May-02 19:41 UTC
[R] Creating Dummy Variables with if else phrase
Dear All, I want to do 52 state dummy variables. In order to make it easier for me, I thought of using the below commands. (x is the vector with state variables, matrix will correspond to the dummy variables) x <- c(1,2,NA,4) matrix <- matrix(0,nrow=4,ncol=4) for (i in 1:4) { if (is.real(x[i])) { matrix[i,x[i]] <- 1 } else { matrix[i,] <- rep(NA,4) } } This gives me the following matrix: 1 0 0 0 0 1 0 0 0 0 0 0 0 0 0 1 But I want it to look as follows: 1 0 0 0 0 1 0 0 NA NA NA NA 0 0 0 1 I want the forth row to be NA, but it is unchanged, i.e., 0. The second "else" phrase does not seem to work, and I cannot see what is wrong. I would be very grateful for your help. Regards, Sophie
On Friday 02 May 2003 21:41, langensk at fas.harvard.edu wrote:> Dear All, > > I want to do 52 state dummy variables. In order to make it easier > for me, I thought of using the below commands. (x is the vector with > state variables, matrix will correspond to the dummy variables) > > > x <- c(1,2,NA,4) > matrix <- matrix(0,nrow=4,ncol=4) > > for (i in 1:4) { > if (is.real(x[i])) { > matrix[i,x[i]] <- 1 } else { > matrix[i,] <- rep(NA,4) > } > } > > This gives me the following matrix: > > 1 0 0 0 > 0 1 0 0 > 0 0 0 0 > 0 0 0 1 > > But I want it to look as follows: > > 1 0 0 0 > 0 1 0 0 > NA NA NA NA > 0 0 0 1 >I don't really understand why you want to create a matrix that way, but if you want to do it the way described above is.real(x[i]) seems to be the wrong function (because x is a real vector in the example above). If you want to test for NA you should use !is.na(x[i]) instead. Z> I want the forth row to be NA, but it is unchanged, i.e., 0. The > second "else" phrase does not seem to work, and I cannot see what is > wrong. > > I would be very grateful for your help. > > Regards, Sophie > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://www.stat.math.ethz.ch/mailman/listinfo/r-help
langensk at fas.harvard.edu writes:> Dear All, > > I want to do 52 state dummy variables. In order to make it easier for me, I > thought of using the below commands. (x is the vector with state variables, > matrix will correspond to the dummy variables) > > > x <- c(1,2,NA,4) > matrix <- matrix(0,nrow=4,ncol=4) > > for (i in 1:4) { > if (is.real(x[i])) { > matrix[i,x[i]] <- 1 } else { > matrix[i,] <- rep(NA,4) > } > } > > This gives me the following matrix: > > 1 0 0 0 > 0 1 0 0 > 0 0 0 0 > 0 0 0 1 > > But I want it to look as follows: > > 1 0 0 0 > 0 1 0 0 > NA NA NA NA > 0 0 0 1 > > > I want the forth row to be NA, but it is unchanged, i.e., 0. The second "else" > phrase does not seem to work, and I cannot see what is wrong. > > I would be very grateful for your help.is.real is true for all elements in x. You may have intended !is.na(x[i]). However, there are simpler ways, e.g. 0+outer(x,1:4,"==") [,1] [,2] [,3] [,4] [1,] 1 0 0 0 [2,] 0 1 0 0 [3,] NA NA NA NA [4,] 0 0 0 1 or simply use model formulas and code x as a factor. -- O__ ---- Peter Dalgaard Blegdamsvej 3 c/ /'_ --- Dept. of Biostatistics 2200 Cph. N (*) \(*) -- University of Copenhagen Denmark Ph: (+45) 35327918 ~~~~~~~~~~ - (p.dalgaard at biostat.ku.dk) FAX: (+45) 35327907
There is a bit of a trap for young players involved, but: > x <- NA > is.real(x) [1] FALSE > x <- c(1,NA) > is.real(x[2]) [1] TRUE > x <- as.real(NA) > is.real(x) [1] TRUE > typeof(x) [1] "double" > x <- NA > typeof(x) [1] "logical" It makes sense when you think it through: An ``NA'' could be anything on its own. When it's an entry of a vector with real entries then it oughta be a ``not available real''. It's a bit subtle when you first bump into it, but it all clicks into place. Be that as it may --- to avoid the trap, use is.na() if you want to check whether a value is missing. cheers, Rolf Turner