This is a test version of Biostars. For the public version, visit https://www.biostars.org.
col as names [solved]

Hello

I have been trying to read the txt file "genes_tpm.txt" as a data frame where the first row is a name with the code

tab3rows <- read.table("gene_tpm.txt", header = TRUE, nrows = 3)

However, I get VX as names (V1, V2, V3...) instead of the first row.

I have tried using

my.names <- tab3rows[1,]
colnames(tab3rows) <- my.names

BUT the colnames of "my.names" are "3, 2, 3, 3, 3, 3...", with a row with the actual names I want, and

colnames(tab3rows) <- my.names

adds those numbers as the names instead...

What am I doing wrong?

r

I'm not sure what you mean by "the colnames of "my.names" are "3, 2, 3, 3, 3, 3...", with a row with the actual names I want"

but read.table will not accept colnames that start with a number. Only column with names that are valid R variable names are allowed. As you have seen though, you can force this by manually setting the column names to numbers, this however is a bad idea, as it becomes very difficult to distinguish between column 3 and the column called 3.

tab3rows

colnames

result

sorry guys, now that I am reading it again I wasn't very clear

first figure is my "tab3rows", and the row 1 is what I wish was read as names. Vxes are being added by R and are not in the original txt, which is from the code

tab3rows <- read.table("gene_tpm.txt", header = TRUE, nrows = 3)

second figure contains the result of

my.names <- tab3rows[1,]
colnames(tab3rows) <- my.names

and the third figure is the result of

colnames(tab3rows) <- my.names

Can you show us the result of head -n 3 gene_tpm.txt | cut -f 1-3 from the command line?

I have got the error

"unexpected numeric constant in "head -n 3"

michael.ante wanted you to run the command inside linux terminal not inside R. What OS are you using?

ooh I should have known that was a bash line now I feel like a double dumbleass, the Vxs are on the first lines and were added by R in a previous instance. I should have known.

terminal

I think I can remove the first line on linux terminal and then all my r codes will work?

You can use in the read.table command the parameter skip=1

I ended up using ex -s -c '1d|x' gene_tpm.txt because that line was irritating me for a while now

thanks everyone, problem solved

Does the first row of your file starts with a '#' ? While testing on an example (R3.4.4), this lead to skipping the first line.

Do you want to have you rownames as colnames?

What does eliminating nrows=3 from read.table() do? Wonder if there's a conflict between header=TRUE and nrows=3... read.table("blah.csv",header=TRUE) should be sufficient.

0 answers

No answers yet.

Log in to answer this question.