0

I have a column as below. Only for non-null elements, I want to get a matrix such as below. 6th column represent the actual value.

1 0 0 0 0 1 0 1 0 0 0 2 0 0 0 1 0 5 

Any hint what is the efficient way to do this? which commands should I use? I am thinking of writing a if loop within for loop, but don't think it will be very efficient :(

abc=c('1','2','null','5','null') 
1
  • Actual value of what? What do the 1s and 0s in the previous 5 columns represent? Commented Mar 31, 2014 at 1:48

2 Answers 2

1

Assuming there is an error in your example, this is just a dummy variable coding essentially:

abc <- c('1','2','null','5','null') abc <- factor(abc,levels=1:5) cbind(model.matrix(~abc+0),orig=na.omit(abc)) # abc1 abc2 abc3 abc4 abc5 orig #1 1 0 0 0 0 1 #2 0 1 0 0 0 2 #4 0 0 0 0 1 5 

If you want to automatically calculate the range of possible factors, try:

abc <- c('1','2','null','5','null') rng <- range(as.numeric(abc),na.rm=TRUE) abc <- factor(abc,levels=seq(rng[1],rng[2])) cbind(model.matrix(~abc+0),orig=na.omit(abc)) # abc1 abc2 abc3 abc4 abc5 orig #1 1 0 0 0 0 1 #2 0 1 0 0 0 2 #4 0 0 0 0 1 5 
Sign up to request clarification or add additional context in comments.

5 Comments

in first line, how can i say that include all levels except na? My original data is huge and i dont want know range of my number..if i change the first line to abc <- factor(abc) then i get extra rows for nulls :(
@user2543622 - you can separately calculate the range - rng <- range(as.numeric(abc),na.rm=TRUE) and use that to determine the levels of the factor: factor(abc,levels=seq(rng[1],rng[2]))
seems not working :( > rng <- range(as.numeric(abc),na.rm=TRUE) Warning message: NAs introduced by coercion
@user2543622 - warnings are not errors, as.numeric just forces non-numbers to NA, that is all.
any help? - in your answer, on line 4, i need "1" in column 4 and not in column 5
1

It's not clear why that matrix is six elements wide but if it is length(abc)+1 then just substitute that expression for my use of 6.

> abcn <- as.numeric(abc) > zero <- matrix(0,nrow=length(abcn[!is.na(abcn)]), ncol=6) > zero[ cbind(1:3, which( !is.na(abcn)) ) ] <- 1 > zero[ , 6] <- abcn[!is.na(abcn)] > zero [,1] [,2] [,3] [,4] [,5] [,6] [1,] 1 0 0 0 0 1 [2,] 0 1 0 0 0 2 [3,] 0 0 0 1 0 5 

You can index teh [<- function for matrices with a two coulmn matrix and that's what I'm doing in the third line. The rest of it is ordinary matrix indexing.

3 Comments

Again we're forced to go off pure unadulterated assumption. Haha.
The main puzzle to me was the fifth column. I wondered if the 1 was suppose to be at [3,5]. We'll see.
six element wide because of length(abc)+1

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.