[R] paste first row string onto every string in column

Patrick Connolly p_connolly at slingshot.co.nz
Wed Aug 12 09:51:22 CEST 2009


On Tue, 11-Aug-2009 at 06:48PM -0700, Jill Hollenbach wrote:

|> 
|> Hi,
|> I am trying to edit a data frame such that the string in the first line is
|> appended onto the beginning of each element in the subsequent rows. The data
|> looks like this:
|> 
|> > df
|>       V1   V2   V3   V4   
|> 1   DPA1* DPA1* DPB1* DPB1*
|> 2   0103 0104 0401 0601 
|> 3   0103 0103 0301 0402 
|> .
|> .
|>  and what I want is this:
|> 
|> >dfnew
|>       V1   V2   V3   V4   
|> 1   DPA1* DPA1* DPB1* DPB1* 
|> 2   DPA1*0103 DPA1*0104 DPB1*0401 DPB1*0601 
|> 3   DPA1*0103 DPA1*0103 DPB1*0301 DPB1*0402 
|> 
|> any help is much appreciated, I am new to this and struggling.


as.data.frame(lapply(df, function(x) paste(x[1], x[-1], sep = "")))

There's a few ideas in there that will get you started.  We could add
a bit more to get the first row you want in one line, but you'll be
able to work that out.

That will end up as a dataframe of factors.  You might need to do
something else with it after that stage.

HTH

-- 
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.   
   ___    Patrick Connolly   
 {~._.~}                   Great minds discuss ideas    
 _( Y )_  	         Average minds discuss events 
(:_~*~_:)                  Small minds discuss people  
 (_)-(_)  	                      ..... Eleanor Roosevelt
	  
~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.




More information about the R-help mailing list