Dear R-experts, I still have problems with the reading of a matrix. Input: matrixData6.txt A-Paar B-Paar C-Paar D-Paar E-Paar A 1 3 5 7 9 B 2 4 6 8 10 R-commands: y=read.table(file="Z:/Software/R-Programme/matrixData6.txt") y Result: A.Paar B.Paar C.Paar D.Paar E.Paar A 1 3 5 7 9 B 2 4 6 8 10 If you look into the txt-file you can recognize that the column names are not the same. Why? If I add in the txt-file the line "MyData:" infront of all followed by a newline. The R-command as above response an error. How can I read the modified input and get the following result: MyData: A.Paar B.Paar C.Paar D.Paar E.Paar A 1 3 5 7 9 B 2 4 6 8 10 Another stupid question might be hows can I change the column and row names after I made "read.table"? I want to have the following result, for example: MyData: G H I J K M 1 3 5 7 9 N 2 4 6 8 10 I studied all manuals I could find and the help but could not understand the examples or interpret it right for my case. Thanks for help, Corinna
Hi r-help-bounces at stat.math.ethz.ch napsal dne 05.04.2007 12:50:09:> Dear R-experts, > > I still have problems with the reading of a matrix. > > Input: matrixData6.txt > > A-Paar B-Paar C-Paar D-Paar E-Paar > A 1 3 5 7 9 > B 2 4 6 8 10 > > > R-commands: > y=read.table(file="Z:/Software/R-Programme/matrixData6.txt") > y > > Result: > A.Paar B.Paar C.Paar D.Paar E.Paar > A 1 3 5 7 9 > B 2 4 6 8 10 > > > If you look into the txt-file you can recognize that the column names > are not the same. Why? > > If I add in the txt-file the line "MyData:" infront of all followed by a > newline. The R-command as above response an error. How can I read the > modified input and get the following result: > > MyData: > > A.Paar B.Paar C.Paar D.Paar E.Paar > A 1 3 5 7 9 > B 2 4 6 8 10The only difference I can see is centering columns and Mydata: row. This does not have anything with reading your data. You shall instead consult how your data is printed on console by looking at ?print help page. However I think that R itself is not the best tool for text formating.> > Another stupid question might be hows can I change the column and row > names after I made "read.table"? I want to have the following result, > for example: > > MyData: > > G H I J K > M 1 3 5 7 9 > N 2 4 6 8 10 > > I studied all manuals I could find and the help but could not understand > the examples or interpret it right for my case.Did you by chance try ?names or ?rownames Regards Petr> > Thanks for help, > > Corinna > > ______________________________________________ > R-help at stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guidehttp://www.R-project.org/posting-guide.html> and provide commented, minimal, self-contained, reproducible code.
Schmitt, Corinna wrote:> > I still have problems with the reading of a matrix. > > Input: matrixData6.txt > > A-Paar B-Paar C-Paar D-Paar E-Paar > A 1 3 5 7 9 > B 2 4 6 8 10 > > > R-commands: > y=read.table(file="Z:/Software/R-Programme/matrixData6.txt") > y > > Result: > A.Paar B.Paar C.Paar D.Paar E.Paar > A 1 3 5 7 9 > B 2 4 6 8 10 > > If you look into the txt-file you can recognize that the column names > are not the same. Why? >Using "_" in variable names was illegal earlier (now this rule seems to be relaxed). read.table has converted names. You could try to change them back (UNTESTED) names(y) <- gsub(".", "_", names(y)), Schmitt, Corinna wrote:> > If I add in the txt-file the line "MyData:" infront of all followed by a > newline. The R-command as above response an error. How can I read the > modified input and get the following result: > > MyData: > > A.Paar B.Paar C.Paar D.Paar E.Paar > A 1 3 5 7 9 > B 2 4 6 8 10 >You could name your variable MyData:> MyData<-read.table( ) > MyDataSchmitt, Corinna wrote:> > Another stupid question might be hows can I change the column and row > names after I made "read.table"? I want to have the following result, > for example: > > MyData: > > G H I J K > M 1 3 5 7 9 > N 2 4 6 8 10 > > I studied all manuals I could find and the help but could not understand > the examples or interpret it right for my case. > > Thanks for help, > Corinna >names(MyData)<-c("G","H","I","J","K") row.names(MyData)<-c("M","N") -- View this message in context: http://www.nabble.com/reading-of-a-matrix-tf3530874.html#a9854003 Sent from the R help mailing list archive at Nabble.com.
On Apr 5, 2007, at 6:50 AM, Schmitt, Corinna wrote:> Dear R-experts, > > I still have problems with the reading of a matrix. > > Input: matrixData6.txt > > A-Paar B-Paar C-Paar D-Paar E-Paar > A 1 3 5 7 9 > B 2 4 6 8 10 > > > R-commands: > y=read.table(file="Z:/Software/R-Programme/matrixData6.txt") > y > > Result: > A.Paar B.Paar C.Paar D.Paar E.Paar > A 1 3 5 7 9 > B 2 4 6 8 10 > > > If you look into the txt-file you can recognize that the column names > are not the same. Why? >Look at ?read.table for details, basically the variable names are turned into syntactically valid names via make.names (?make.names). This is, I pressume, so that you can later refer to them via: df $A.Paar (df$A-Paar means something very different). You can try to add: check.names=FALSE to the read.table call, not sure it will do what you want it to.> If I add in the txt-file the line "MyData:" infront of all followed > by a > newline. The R-command as above response an error. How can I read the > modified input and get the following result:Hm, if you really want the MyData to show up in the result, then you will have to do some more hard work, since data frames don't really have a room for that. But if you simply want MyData: to show up in the text file but not be read by R, then you would want to prepend the line with the comment character, #.> MyData: > > A.Paar B.Paar C.Paar D.Paar E.Paar > A 1 3 5 7 9 > B 2 4 6 8 10 > > Another stupid question might be hows can I change the column and row > names after I made "read.table"? I want to have the following result, > for example: >names(yourdf) <- c("G","H","I","J","K") or perhaps better: names(yourdf) <- LETTERS[6+1:5] That's for the columns. Use ?rownames for the rows.> MyData: > > G H I J K > M 1 3 5 7 9 > N 2 4 6 8 10 > > I studied all manuals I could find and the help but could not > understand > the examples or interpret it right for my case. > > Thanks for help, > > Corinna >Haris Skiadas Department of Mathematics and Computer Science Hanover College
Thank you very much. It realizes the things I want. Thanks a lot, Corinna -----Urspr?ngliche Nachricht----- Von: r-help-bounces at stat.math.ethz.ch [mailto:r-help-bounces at stat.math.ethz.ch] Im Auftrag von Vladimir Eremeev Gesendet: Donnerstag, 5. April 2007 13:25 An: r-help at stat.math.ethz.ch Betreff: Re: [R] reading of a matrix Schmitt, Corinna wrote:> > I still have problems with the reading of a matrix. > > Input: matrixData6.txt > > A-Paar B-Paar C-Paar D-Paar E-Paar > A 1 3 5 7 9 > B 2 4 6 8 10 > > > R-commands: > y=read.table(file="Z:/Software/R-Programme/matrixData6.txt") > y > > Result: > A.Paar B.Paar C.Paar D.Paar E.Paar > A 1 3 5 7 9 > B 2 4 6 8 10 > > If you look into the txt-file you can recognize that the column names > are not the same. Why? >Using "_" in variable names was illegal earlier (now this rule seems to be relaxed). read.table has converted names. You could try to change them back (UNTESTED) names(y) <- gsub(".", "_", names(y)), Schmitt, Corinna wrote:> > If I add in the txt-file the line "MyData:" infront of all followed by a > newline. The R-command as above response an error. How can I read the > modified input and get the following result: > > MyData: > > A.Paar B.Paar C.Paar D.Paar E.Paar > A 1 3 5 7 9 > B 2 4 6 8 10 >You could name your variable MyData:> MyData<-read.table( ) > MyDataSchmitt, Corinna wrote:> > Another stupid question might be hows can I change the column and row > names after I made "read.table"? I want to have the following result, > for example: > > MyData: > > G H I J K > M 1 3 5 7 9 > N 2 4 6 8 10 > > I studied all manuals I could find and the help but could not understand > the examples or interpret it right for my case. > > Thanks for help, > Corinna >names(MyData)<-c("G","H","I","J","K") row.names(MyData)<-c("M","N") -- View this message in context: http://www.nabble.com/reading-of-a-matrix-tf3530874.html#a9854003 Sent from the R help mailing list archive at Nabble.com. ______________________________________________ R-help at stat.math.ethz.ch 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.