Entering edit mode
4.2 years ago
demoraesdiogo2017 ▴ 90
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?
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?
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.
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
second figure contains the result of
and the third figure is the result of
Can you show us the result of
head -n 3 gene_tpm.txt | cut -f 1-3from 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.
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
I ended up using
ex -s -c '1d|x' gene_tpm.txtbecause that line was irritating me for a while now
thanks everyone, problem solved
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.