Problem In running read.Table function in R
3
0
Entering edit mode
8.8 years ago
Vinay Singh ▴ 50

I am reading a text file containing a table so I have used read.table function in R and getting the problem.

Error in FUN(left) : invalid argument type

This is the command I have used for reading table.

data = read.table("intensity.txt", header=T, stringsAsFactors=F)

The format of text file is as (Intensity value)

 control.drought  control.heat  drought.control  control.combined 
1 -0.5817744498 -10.0890279047 0.5817744498 -11.0284839686
2 -0.3181022766 -9.0243890594 0.3181022766 -10.2859574516
3 -0.2690112456 -8.0796832998 0.2690112456 -9.0171546861
4 -0.1847218231 -3.7873259884 0.1847218231 -4.3059746476
5 -0.2024207047 -3.8210416657 0.2024207047 -4.3258501175
6 -0.6141010588 -4.4140746914 0.6141010588 -5.0397997135
7 -2.2505578122 -10.1398011434 2.2505578122 -10.1410584892
8 -0.3309324672 -4.0731881207 0.3309324672 -4.238998183
9 -0.4096105152 -3.8298292653 0.4096105152 -4.0147223178
10  -0.0036240574 -2.8959797594 0.0036240574 -3.0966125001
R microarray data analysis • 11k views
ADD COMMENT
2
Entering edit mode

There is no probable reason, that you should have this error. Try labelling the first column (rowname/serial numbercol) with a name and read again?

ADD REPLY
1
Entering edit mode

Can't reproduce the error. Upload sample data if still you are in trouble.

ADD REPLY
1
Entering edit mode

I believe it will not be produced if you are trying to reproduce it in vi editor or nano, if you try to do it local machine with any text editor sometimes the file encoding changes. Probably the OP was trying to use text editor in local machine rather than in terminal and then run R locally in their machine. I did not have any problems when I ran it in through terminal while it was reproducible when I simply copied the data and pasted in the .txt file, only to find out that the file had different encoding and that the local R was unable to figure that out so the error. If you edit the .txt file to standard encoding it works of ask the local R to recognize it as the standard encoding.

ADD REPLY
0
Entering edit mode

Thanks @ Sukhdeep, BioM, vchris_ngs for your valuable time.

I have tried as suggested by vchris_ngs but still getting the same error as before.

ADD REPLY
2
Entering edit mode

Can you share the file with us (dropbox/drive/gist)? and in R type : sessionInfo() and paste the output here.

ADD REPLY
1
Entering edit mode

Can you tell me what is the file encoding of your intensity.txt, I checked it again. I just copied the data from your question and pasted in a text editor and checked the file encoding to UTF-8 and it simply working with the below commands. It is only the problem of encoding. If you are working on mac then use text wrangler to see the file encoding else see notepad++ for windows. This happens if you use mostly excel or convert a file from one format to another by simply copy pasting its contents and changing the extensions. So just open the .txt in any of the above above mentioned text editor to do the needful and use any of the below commands

I just put the data in this way

number  control.drought    control.heat    drought.control  control.combined
1       -0.58177445        -10.0890279     0.58177445       -11.02848397
2       -0.318102277       -9.024389059    0.318102277      -10.28595745
3       -0.269011246       -8.0796833      0.269011246      -9.017154686
4       -0.184721823       -3.787325988    0.184721823      -4.305974648
5       -0.202420705       -3.821041666    0.202420705      -4.325850118
6       -0.614101059       -4.414074691    0.614101059      -5.039799714
7       -2.250557812       -10.13980114    2.250557812      -10.14105849
8       -0.330932467       -4.073188121    0.330932467      -4.238998183
9       -0.409610515       -3.829829265    0.409610515      -4.014722318
10      -0.003624057       -2.895979759    0.003624057      -3.0966125
data<-read.delim("/path/test1.txt",sep="\t",header=T,stringsAsFactors=F, fileEncoding ="UTF-8")
data<-read.table("/path/test1.txt",sep="\t",header=T,stringsAsFactors=F, fileEncoding ="UTF-8")
ADD REPLY
0
Entering edit mode

Thanks you all I have started another session of R and now its working fine.

But here comes the new problem

data = read.table("Intensity.txt", header=T, stringsAsFactors=F)
data1 <-as.matrix(data)
heatmap(as.matrix(data1))

Error in heatmap(as.matrix(data1)) : 'x' must be a numeric matrix

Please help me solve this problem.

ADD REPLY
0
Entering edit mode

Thanks all for helping me in getting the results.

Finally I got heat map of DE genes in a microarray experiment.

ADD REPLY
3
Entering edit mode
8.8 years ago
ivivek_ngs ★ 5.2k

It is a problem of the fileEncoding . So open the .txt file in notepad or any text editor to change the file-encoding to any standard version like UTF-8 then it should work. This is the reason you are having the problem.

ADD COMMENT
2
Entering edit mode
8.8 years ago

RUN a

sapply(data1, class)

to check the classes of your data. This way you will know whether your data are numeric or character

ADD COMMENT
0
Entering edit mode

Thanks Antonio for your help,

*Yes it is showing Character, now how can i change it to numeric? *

 -0.581774450  -0.318102277  -0.269011246  -0.184721823  -0.202420705
  "character"   "character"   "character"   "character"   "character"
 -0.614101059  -2.250557812  -0.330932467  -0.409610515  -0.003624057
  "character"   "character"   "character"   "character"   "character"
   -10.089028     -9.024389     -8.079683     -3.787326     -3.821042
  "character"   "character"   "character"   "character"   "character"
    -4.414075    -10.139801     -4.073188     -3.829829     -2.895980
  "character"   "character"   "character"   "character"   "character"
  0.581774450   0.318102277   0.269011246   0.184721823   0.202420705
  "character"   "character"   "character"   "character"   "character"
  0.614101059   2.250557812   0.330932467   0.409610515   0.003624057
  "character"   "character"   "character"   "character"   "character"
   -11.028484    -10.285957     -9.017155     -4.305975     -4.325850
  "character"   "character"   "character"   "character"   "character"
    -5.039800    -10.141058     -4.238998     -4.014722     -3.096613
ADD REPLY
0
Entering edit mode

Now you can see the source of the error. Your numeric values are character

You have several choices

  1. In the code of read.table include this option as.is = TRUE

    I think this will be enough fot this time and for future use of the read.table() function

  2. Or in this case, after reading the table or at the time of creating the matrix include a as.numeric()

    data1 <-as.matrix(as.numeric(data))
    

After that, run a sapply(your_data, class) again to check if everything is OK

ADD REPLY
0
Entering edit mode

Probably the reason why all the columns are characters is because one or more rows contain some non-numeric values. I would search the files for empty lines or comments. Another way to identify the faulty lines is to search of NAs in the data1 matrix.

ADD REPLY
0
Entering edit mode

I have used this script to change the data into numeric data

data.frame(lapply(data1,as.numeric))

but now getting other error,

heatmap(as.matrix(data.frame))
Error in as.vector(x, mode) :

cannot coerce type 'closure' to vector of type 'any'

If any one can help me in getting desired results, it would be a great help.

ADD REPLY
0
Entering edit mode

Some heatmaps works only with matrix, not data.frame

Give it a try

ADD REPLY
1
Entering edit mode
8.8 years ago
Mo ▴ 920

I load your data without any problem

data <- read.table("path to your data /data.txt", header=TRUE, quote="\"")

#  control.drought control.heat drought.control control.combined
#1     -0.581774450   -10.089028     0.581774450       -11.028484
#2     -0.318102277    -9.024389     0.318102277       -10.285957
#3     -0.269011246    -8.079683     0.269011246        -9.017155
#4     -0.184721823    -3.787326     0.184721823        -4.305975
#5     -0.202420705    -3.821042     0.202420705        -4.325850
#6     -0.614101059    -4.414075     0.614101059        -5.039800
#7     -2.250557812   -10.139801     2.250557812       -10.141058
#8     -0.330932467    -4.073188     0.330932467        -4.238998
#9     -0.409610515    -3.829829     0.409610515        -4.014722
#10    -0.003624057    -2.895980     0.003624057        -3.096613
ADD COMMENT

Login before adding your answer.

Traffic: 865 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