col as names [solved]
0
0
Entering edit mode
5.7 years ago

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 • 2.0k views
ADD COMMENT
2
Entering edit mode

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?

ADD REPLY
1
Entering edit mode

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.

ADD REPLY
0
Entering edit mode

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
ADD REPLY
1
Entering edit mode

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

ADD REPLY
0
Entering edit mode

I have got the error

"unexpected numeric constant in "head -n 3"

ADD REPLY
2
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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?

ADD REPLY
1
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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

ADD REPLY
0
Entering edit mode

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.

ADD REPLY

Login before adding your answer.

Traffic: 2332 users visited in the last hour
Help About
FAQ
Access RSS
API
Stats

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.

Powered by the version 2.3.6