This is a test version of Biostars. For the public version, visit https://www.biostars.org.
How to define a column class in R, dataframe

Hi,

I asked this question in other forums but couldn't get the answer for it, I hope somebody can help me here.

I have a data frame like this

scf0001    123    4567   -  4350
scf0001    4474   4878   +  376  *
scf0002    5375   10571  c  5006
scf0003    11370  16986  c  5536
scf0003    16256  17000  -  789  *

when I attempt to read it as a table in R, the error is line 2 did not have 5 elements, so it is not recognizing the star(), I tried to read it as string with `na.string="" but it didn't work, so I think I have to define it withcolclasses=. I tried something like thiscolClasses=[, c(6)="character"]to define the 6th column of the table but it doesn't work,Error: unexpected '['`

The only answer I received was to use fill = TRUE but what it did was to create a new row for the stars rather than defining them as the last column

scf0001    123    4567   -  4350
scf0001    4474   4878   +  376
*          NA      NA    NA   NA
scf0002    5375   10571  c  5006

any idea how to work around this?

colclasses r dataframe

1 answer

> read.table("test.txt", fill = T, row.names = NULL, header = F)

       V1    V2    V3 V4   V5 V6
1 scf0001   123  4567  - 4350   
2 scf0001  4474  4878  +  376  *
3 scf0002  5375 10571  c 5006   
4 scf0003 11370 16986  c 5536   
5 scf0003 16256 17000  -  789  *

Log in to answer this question.