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") > idtYear 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> rError: object "r" not found Kindly advise me as to how to get rid of the error message. AJSS
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.
On Fri, 23-May-2008 at 08:40PM -0700, amarjit singh sethi wrote: |> Dear all, |> I am quite new to R; facing certain problems: |> Say, I have a text file( named as "try"): |> > 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 The help file for cor says this about x and y: x: a numeric vector, matrix or data frame. y: 'NULL' (default) or a vector, matrix or data frame with compatible dimensions to 'x'. The default is equivalent to 'y = x' (but more efficient). Your x isn't a numeric vector, matrix or data frame. x[,-1] is, and that might give what you want, but read the rest of the help file to determine if that's what you want it to do. Having rownames on your dataframe might help seeing what is happening. The easiest way I can see to get row names is in your read.table line. Add row.names = 1. HTH -- ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~. ___ Patrick Connolly {~._.~} Great minds discuss ideas _( Y )_ Middle minds discuss events (:_~*~_:) Small minds discuss people (_)-(_) ..... Anon ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
Maybe Matching Threads
- Iterative estimation of linear regression model
- mfrow
- [R-pkgs] New package: `lavaan' for latent variable analysis (including structural equation modeling)
- who happenly read these two paper Mohsen Pourahmadi (biometrika1999, 2000)
- Getting names of variables without quotes