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

Don MacQueen macq at llnl.gov
Wed Aug 12 17:50:09 CEST 2009

```Let's start with something simple and relatively easy to understand,
since you're new to this.

First, here's an example of the core of the idea:
>  paste('a',1:4)
[1] "a 1" "a 2" "a 3" "a 4"

Make it a little closer to your situation:
>  paste('a*',1:4, sep='')
[1] "a*1" "a*2" "a*3" "a*4"

Sometimes it helps to save the number of rows in your dataframe in a
new variable

nr <- nrow(df)

Then, for your first column, the "a*" in the above example is df\$V1[1]
For the 1:4 in the example, you use  df\$V1[ 2:nr]
Put it together and you have:

dfnew <- df
dfnew\$V1[ 2:nr] <- paste( dfnew\$V1[1], dfnew\$V1[ 2:nr] )

But you can use "-1" instead of "2:nr", and you get

dfnew\$V1[ -1 ] <- paste( dfnew\$V1[1], dfnew\$V1[ -1] )

That's how you can do it one column at a time.
Since you have only four columns, just do the same thing to V2, V3, and V4.

But if you want a more general method, one that works no matter how
many columns you have, and no matter what they are named, then you
can use lapply() to loop over the columns. This is what Patrick
Connolly suggested, which is

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

Note, though, that this will do it to all columns, so if you ever
happen to have a dataframe where you don't want to do all columns,
you'll have to be a little trickier with the lapply() solution.

-Don

At 6:48 PM -0700 8/11/09, 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.
>Jill
>
>___
>  Jill Hollenbach, PhD, MPH
>     Assistant Staff Scientist
>     Center for Genetics
>     Children's Hospital Oakland Research Institute
>     jhollenbach at chori.org
>
>--
>View this message in context:
>http://*www.*nabble.com/paste-first-row-string-onto-every-string-in-column-tp24928720p24928720.html
>Sent from the R help mailing list archive at Nabble.com.
>
>______________________________________________
>R-help at r-project.org mailing list
>https://*stat.ethz.ch/mailman/listinfo/r-help
>and provide commented, minimal, self-contained, reproducible code.

--
--------------------------------------
Don MacQueen
Environmental Protection Department
Lawrence Livermore National Laboratory
Livermore, CA, USA
925-423-1062

```