3

I have a data frame called genalex, because I am trying to put my genetic data into the common "genalex" format. I just used the strsplit function in R, to split columns, and now I have this:

> genalex[1:5,1:10]
  Ind V1 V2 V3 V4 V5 V6 V7 V8 V9
1 100  A  A  C  C  N  N  C  C  N
2 101  A  A  C  C  N  N  N  N  N
3  10  A  A  C  C  N  N  C  C  N
4  11  A  A  N  N  N  N  C  C  N
5  12  N  N  N  N  N  N  C  C  G

This data frame actually has 330 rows and 32,068 columns. I would like to replace the name of every other column (V1, V3, V5, V7, V9, etc.) with a NEW name. I would also like to delete every other column name (V2, V4, V6, V8, etc.)

For the new column names, I would like to use the column names from my old data frame, before I split the columns. I will call the old data frame table. This data frame has 16,034 columns, because it does not include the spaces:

 table[1:5,1:5]
    X X446043 X539052 X614054 X683054
1 100       A       C       N       C
2 101       A       C       N       N
3  10       A       C       N       C
4  11       A       N       N       C
5  12       N       N       N       C

Therefore, using the old column names from table, and then adding a space between each column, I ultimately would like for my data frame to look like this, for all 32,068 columns:

   > genalex[1:5,1:8]
      Ind    X446043         X539052         X614054         X683054
    1 100    A       A       C       C       N       N       C  
    2 101    A       A       C       C       N       N       N  
    3  10    A       A       C       C       N       N       C  
    4  11    A       A       N       N       N       N       C  
    5  12    N       N       N       N       N       N       C  

Thank you for any help.

user3545679
  • 181
  • 1
  • 12

1 Answers1

5

You can use the seq function to get the odd and even positions:

names(genalex)[seq(2,ncol(genalex),2)] <- names(table)
names(genalex)[seq(1,ncol(genalex),2)] <- ""
names(genalex)[1] <- "Ind"

The first line renames all the even-numbered column indices (2,4,6,...) with the column names from table and the second sets all the odd-numbered column indices to blank. Then rename the first column to "Ind".

mattdevlin
  • 1,045
  • 2
  • 10
  • 17
  • Thank you; just to be clear, decimals are used as placeholders for blanks? These are still treated as blanks within R? – user3545679 Jan 03 '16 at 01:01
  • @user3545679: decimals? I don't understand your question. – mattdevlin Jan 03 '16 at 01:41
  • Instead of getting blanks for the "blank" columns, I get decimal points as headings. When I try to add NA's instead of blanks, using this code: names(genalex)[seq(1,ncol(genalex),2)] <- "NA" then my "blank" columns result in: NA, NA.1, NA.2, NA.3. Still decimals!? – user3545679 Jan 03 '16 at 02:00
  • I'm not sure why that would happen. Try setting all columns to "": `names(genalex) <- rep("", ncol(genalex))` then the first and third lines of the above code. – mattdevlin Jan 03 '16 at 02:42