[R] Importing data in text file into R

John Fox jfox at mcmaster.ca
Sat May 24 06:06:47 CEST 2008


Dear AJSS,

The problem is that the way you've read the data, the first column of  
the data frame is a factor, not a numerical variable, and thus is not  
suitable for computing correlations.

You could use the command cor(idt[,-1]) to compute correlations on all  
but the first column, but your intention was probably to use the first  
column of the input file for row names, not as a variable. One way to  
do this is simply to omit the variable name "Year" in the first row of  
the input data file.

I hope this helps,
  John

On 23-May-08, at 11:40 PM, amarjit singh sethi wrote:

> Dear all,
> I am quite new to R; facing certain problems:
> Say, I have a text file( named as "try"):
> Year	C1	C2	C3	C4	C5	C6
> Y1	3.5	13.8	9.5	6.8	0.4	24.2
> Y2	3.8	13.9	9.9	7.6	0.7	12.8
> Y3	4.5	14.5	14.2	9.2	0.6	14.5
> Y4	5.9	16.2	24.6	12.7	0.2	24.3
> Y5	7.2	20.4	40.6	18.2	0.8	28.2
> Y6	5.9	18.6	37.4	14.5	0.3	36.9
> Y7	8.0	16.1	88.6	24.1	0.1	34.6
> Y8	13.6	21.1	56.3	19.0	0.7	33.3
>
> I wish to import the file into R and make certain
> computations, like intercorrelation matrix. I tried
> the following syntax:
>
> # Inputting the data file (saved in text format)
> df="trial.txt"
> idt=read.table(df,header=T, sep="\t")
> idt
> # To generate intercorrelatio matrix
> r = cor(idt)
> r=round(r, 4)
> r
>
> The seems to have been read, but further computations
> not made, with the following output:
>
>> # Inputting the data file (saved in text format)
>> df="trial.txt"
>> idt=read.table(df,header=T, sep="\t")
>> idt
>  Year   C1   C2   C3   C4  C5   C6
> 1   Y1  3.5 13.8  9.5  6.8 0.4 24.2
> 2   Y2  3.8 13.9  9.9  7.6 0.7 12.8
> 3   Y3  4.5 14.5 14.2  9.2 0.6 14.5
> 4   Y4  5.9 16.2 24.6 12.7 0.2 24.3
> 5   Y5  7.2 20.4 40.6 18.2 0.8 28.2
> 6   Y6  5.9 18.6 37.4 14.5 0.3 36.9
> 7   Y7  8.0 16.1 88.6 24.1 0.1 34.6
> 8   Y8 13.6 21.1 56.3 19.0 0.7 33.3
>> # To generate intercorrelatio matrix
>> r = cor(idt)
> Error in cor(idt) : missing observations in cov/cor
> In addition: Warning message:
> In cor(idt) : NAs introduced by coercion
>> r=round(r, 4)
> Error: object "r" not found
>> r
> Error: object "r" not found
>
> Kindly advise me as to how to get rid of the error
> message.
>
> AJSS
>
> ______________________________________________
> R-help at r-project.org mailing list
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.



More information about the R-help mailing list