[R] Insert elements into a vector in a defined positions

Manuel Ramon manugen at gmail.com
Thu Nov 26 18:29:41 CET 2009


That's good . Your solution works for me.
Than you Rolf.



Rolf Turner-3 wrote:
> 
> 
> On 26/11/2009, at 10:46 AM, Manuel Ramon wrote:
> 
>>
>> Dear R users,
>> I have a vector of length n and I want to insert some elements (in  
>> my case
>> the NA string) into a defined positions. For example, my vector is  
>> z1 and I
>> want to add NA's in positions 4, 6 y 7 so after that, my new  
>> vector, z2,
>> should have a length of 10+3.
>>
>>   z1 <- 1:10
>>   id <- c(4,6,7)
>>
>>   # And z2 should be:
>>   z2 <- c(1,2,3,NA,4,5,NA,NA,6,7,8,9,10)
>>
>> Anyone knows how can I do that?
> 
> At first I thought append() might work ... but the "after"
> argument to append seems to have to be of length 1.
> 
> How about:
> 
> 	z2 <- numeric(length(z1)+length(id))
> 	z2[id] <- NA
> 	z2[!is.na(z2)] <- z1
> 
> A bit kludgy, but it appears to work.
> 
> 	cheers,
> 
> 		Rolf Turner
> 
> ######################################################################
> Attention:\ This e-mail message is privileged and confid...{{dropped:9}}
> 
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide
> http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
> 
> 


-----
Manuel Ramón Fernández
Group of Reproductive Biology (GBR)
University of Castilla-La Mancha (Spain)
mramon at jccm.es
-- 
View this message in context: http://old.nabble.com/Insert-elements-into-a-vector-in-a-defined-positions-tp26520841p26532643.html
Sent from the R help mailing list archive at Nabble.com.




More information about the R-help mailing list