1

I have a summary of my data in the form of data frame

      V1 V3       V4     V5 V6       V7     V8 V9      V10    V11 V12      V13    V14
1   CCL5  P 0.000491 6546.2  P 0.000491 6546.2  P 0.000491 6546.2   P 0.000491 6546.2
2  EPHB3  P 0.000562  461.3  P 0.000562  461.3  P 0.000562  461.3   P 0.000562  461.3
3 GUCA1A  A 0.602006    9.4  A 0.602006    9.4  A 0.602006    9.4   A 0.602006    9.4
4  HSPA6  P 0.000322  564.0  P 0.000322  564.0  P 0.000322  564.0   P 0.000322  564.0
5 PTPN21  A 0.204022   94.3  A 0.204022   94.3  A 0.204022   94.3   A 0.204022   94.3
6   UBA7  P 0.000468  845.6  P 0.000468  845.6  P 0.000468  845.6   P 0.000468  845.6
7  7-Mar  P 0.000673  643.2  P 0.000673  643.2  P 0.000673  643.2   P 0.000673  643.2

I know very simple case of naming the columns by using something like

names(df)= c("Gene","Score","Call") ## df a data frame with three columns

But here the case is a bit different than simple case, where the first column is global which i want to assign name gene and after that for 2nd column call, for 3rd p-value, and forth intensity and repeat it till done.

The final output should look something like this

   Gene    call   p-value     intensity   call  p-value     intensity       call  p-value     intensity  call   p-value     intensity
1   CCL5     P    0.000491    6546.2       P    0.000491    546.2            P    0.000491    6546.2       P    .000491      6546.2
2   EPHB3    P    0.000562    461.3        P    0.000562    461.3            P    0.000562    461.3        P    0.000562     461.3
3  GUCA1A    A    0.602006    9.4          A    0.602006    9.4              A    0.602006    9.4          A    0.602006     9.4
4  HSPA6     P    0.000322    564.0        P    0.000322    564.0            P    0.000322    564.0        P    0.000322     564.0
5 PTPN21     A    0.204022    94.3         A    0.204022    94.3             A    0.204022    94.3         A    0.204022     94.3
6 UBA7       P    0.000468    845.6        P    0.000468    845.6            P    0.000468    845.6        P    0.000468     845.6 
7  7-Mar     P    0.000673    643.2        P    0.000673    643.2            P    0.000673    643.2        P    0.000673     643.2

Edited: Column number is not known

2 Answers 2

3

you can try this

names(df) <- c("Gene", rep(c("call", "p-value", "intensity"), 4))
    Gene call  p-value intensity call  p-value intensity call  p-value intensity call  p-value intensity
1   CCL5    P 0.000491    6546.2    P 0.000491    6546.2    P 0.000491    6546.2    P 0.000491    6546.2
2  
...
Sign up to request clarification or add additional context in comments.

4 Comments

Note, however, that non-unique column names might result in problems later on. Thus, this is the correct answer, but I would not want to use the result.
Use rep with argument times = (ncol(df) - 1)%/%3 for unknown length
Shows this : Error in names(df) <- c("Gene", rep(c("call", "p-value", "intensity"), : 'names' attribute [13] must be the same length as the vector [4]
@jdharrison, as my column number is not known, would U please like to share the full line of code.
0

This also works as per comment posted @jdharrison

names(df) <- c("Gene", rep(c("call", "p-value", "intensity"), times = (ncol(df) - 1)%/%3))

    Gene call  p-value intensity call  p-value intensity call  p-value intensity call  p-value intensity
1   CCL5    P 0.000491    6546.2    P 0.000491    6546.2    P 0.000491    6546.2    P 0.000491    6546.2
2  EPHB3    P 0.000562     461.3    P 0.000562     461.3    P 0.000562     461.3    P 0.000562     461.3
3 GUCA1A    A 0.602006       9.4    A 0.602006       9.4    A 0.602006       9.4    A 0.602006       9.4
4  HSPA6    P 0.000322     564.0    P 0.000322     564.0    P 0.000322     564.0    P 0.000322     564.0
5 PTPN21    A 0.204022      94.3    A 0.204022      94.3    A 0.204022      94.3    A 0.204022      94.3
6   RFC2    P 0.000673     643.2    P 0.000673     643.2    P 0.000673     643.2    P 0.000673     643.2
7   UBA7    P 0.000468     845.6    P 0.000468     845.6    P 0.000468     845.6    P 0.000468     845.6

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.