Question: col as names [solved]
0
gravatar for demoraesdiogo2017
5 weeks ago by
demoraesdiogo201710 wrote:

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?

column as names read.table • 191 views
ADD COMMENTlink modified 5 weeks ago • written 5 weeks ago by demoraesdiogo201710
2

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 REPLYlink modified 5 weeks ago • written 5 weeks ago by michael.ante3.2k
1

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 REPLYlink written 5 weeks ago by i.sudbery4.3k

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 REPLYlink written 5 weeks ago by demoraesdiogo201710
1

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

ADD REPLYlink written 5 weeks ago by michael.ante3.2k

I have got the error

"unexpected numeric constant in "head -n 3"

ADD REPLYlink written 5 weeks ago by demoraesdiogo201710
2

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

ADD REPLYlink written 5 weeks ago by ahaswer150

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 REPLYlink modified 5 weeks ago • written 5 weeks ago by demoraesdiogo201710
1

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

ADD REPLYlink written 5 weeks ago by michael.ante3.2k

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 REPLYlink written 5 weeks ago by demoraesdiogo201710

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 REPLYlink modified 5 weeks ago • written 5 weeks ago by ctseto50
Please log in to add an answer.

Help
Access

Use of this site constitutes acceptance of our User Agreement and Privacy Policy.
Powered by Biostar version 2.3.0
Traffic: 1094 users visited in the last hour