Patrick Schorderet
2009-Jul-29 08:29 UTC
[R] Newbie in R: Reading .txt files and storing the 'numbers' in a vector
Hello everybody, I'm a newbie in R and just went through an introduction class recently. Here's my problem. I have 2 text files (.txt) with plain numbers ('doubles' for those who know c++) ordered into 2 columns as below: coordinate1 value1 coordinate2 value2 coordinate3 value3 ... ... coordinateN valueN I would like to write a small programme in which i would: 1. take all values of file 1 and store them in a vector 2. substract to those all values of file 2 (one by one) and store the result in a new vector 3. Create a sliding window of chosen size and calculate the mathematical average and store these averages in a new vector 4. Plot the different graphes with a bar plot So I wrote a c++ script that does the first parts and writes the averages in a output.txt file (which I have up and running). Unfortunately, I seem to fail in reading these as numbers and putting them into a vector (in the R environment). I can read the file , but this is what I get: > z=scan(file = "/Users/NCCRGENETICS/Desktop/testRnumbers.txt", what = "double") Read 19 items > z [1] "{\\rtf1\\ansi\\ansicpg1252\\cocoartf949\\cocoasubrtf430" [2] "{\\fonttbl\\f0\\fswiss\\fcharset0" [3] "Helvetica;}" [4] "{\\colortbl;\\red255\\green255\\blue255;}" [5] "\\paperw11900\\paperh16840\\margl1440\\margr1440\\vieww9000\ \viewh8400\\viewkind0" [6] "\\pard\\tx566\\tx1133\\tx1700\\tx2267\\tx2834\\tx3401\\tx3968\ \tx4535\\tx5102\\tx5669\\tx6236\\tx6803\\ql\\qnatural\\pardirnatural" [7] "\\f0\\fs24" [8] "\\cf0" [9] "13\\" // This is where my file "should" start reading the numbers I see in the window when I open it with textedit [10] "27\\" [11] "39\\" [12] "48\\" [13] "34\\" [14] "17\\" [15] "23\\" [16] "45\\" [17] "23\\" [18] "4\\" [19] "}" > Thanks so much to any of you who could provoid a little help... I'm getting nuts. Patrick
ONKELINX, Thierry
2009-Jul-29 08:34 UTC
[R] Newbie in R: Reading .txt files and storing the 'numbers' in avector
Have a look at ?read.table HTH, Thierry ------------------------------------------------------------------------ ---- ir. Thierry Onkelinx Instituut voor natuur- en bosonderzoek / Research Institute for Nature and Forest Cel biometrie, methodologie en kwaliteitszorg / Section biometrics, methodology and quality assurance Gaverstraat 4 9500 Geraardsbergen Belgium tel. + 32 54/436 185 Thierry.Onkelinx at inbo.be www.inbo.be To call in the statistician after the experiment is done may be no more than asking him to perform a post-mortem examination: he may be able to say what the experiment died of. ~ Sir Ronald Aylmer Fisher The plural of anecdote is not data. ~ Roger Brinner The combination of some data and an aching desire for an answer does not ensure that a reasonable answer can be extracted from a given body of data. ~ John Tukey -----Oorspronkelijk bericht----- Van: r-help-bounces at r-project.org [mailto:r-help-bounces at r-project.org] Namens Patrick Schorderet Verzonden: woensdag 29 juli 2009 10:29 Aan: r-help at r-project.org Onderwerp: [R] Newbie in R: Reading .txt files and storing the 'numbers' in avector Hello everybody, I'm a newbie in R and just went through an introduction class recently. Here's my problem. I have 2 text files (.txt) with plain numbers ('doubles' for those who know c++) ordered into 2 columns as below: coordinate1 value1 coordinate2 value2 coordinate3 value3 ... ... coordinateN valueN I would like to write a small programme in which i would: 1. take all values of file 1 and store them in a vector 2. substract to those all values of file 2 (one by one) and store the result in a new vector 3. Create a sliding window of chosen size and calculate the mathematical average and store these averages in a new vector 4. Plot the different graphes with a bar plot So I wrote a c++ script that does the first parts and writes the averages in a output.txt file (which I have up and running). Unfortunately, I seem to fail in reading these as numbers and putting them into a vector (in the R environment). I can read the file , but this is what I get: > z=scan(file = "/Users/NCCRGENETICS/Desktop/testRnumbers.txt", what "double") Read 19 items > z [1] "{\\rtf1\\ansi\\ansicpg1252\\cocoartf949\\cocoasubrtf430" [2] "{\\fonttbl\\f0\\fswiss\\fcharset0" [3] "Helvetica;}" [4] "{\\colortbl;\\red255\\green255\\blue255;}" [5] "\\paperw11900\\paperh16840\\margl1440\\margr1440\\vieww9000\ \viewh8400\\viewkind0" [6] "\\pard\\tx566\\tx1133\\tx1700\\tx2267\\tx2834\\tx3401\\tx3968\ \tx4535\\tx5102\\tx5669\\tx6236\\tx6803\\ql\\qnatural\\pardirnatural" [7] "\\f0\\fs24" [8] "\\cf0" [9] "13\\" // This is where my file "should" start reading the numbers I see in the window when I open it with textedit [10] "27\\" [11] "39\\" [12] "48\\" [13] "34\\" [14] "17\\" [15] "23\\" [16] "45\\" [17] "23\\" [18] "4\\" [19] "}" > Thanks so much to any of you who could provoid a little help... I'm getting nuts. Patrick ______________________________________________ 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. Dit bericht en eventuele bijlagen geven enkel de visie van de schrijver weer en binden het INBO onder geen enkel beding, zolang dit bericht niet bevestigd is door een geldig ondertekend document. The views expressed in this message and any annex are purely those of the writer and may not be regarded as stating an official position of INBO, as long as the message is not confirmed by a duly signed document.
Patrick Connolly
2009-Jul-29 20:37 UTC
[R] Newbie in R: Reading .txt files and storing the 'numbers' in a vector
On Wed, 29-Jul-2009 at 10:29AM +0200, Patrick Schorderet wrote:> > Hello everybody, > > I'm a newbie in R and just went through an introduction class recently.Did you get the distinction between a vector and a dataframe?> > Here's my problem. > I have 2 text files (.txt) with plain numbers ('doubles' for those who > know c++) ordered into 2 columns as below: > > coordinate1 value1 > coordinate2 value2 > coordinate3 value3 > ... ... > coordinateN valueN > > I would like to write a small programme in which i would: > 1. take all values of file 1 and store them in a vectorI'm fairly certain you need a dataframe, hence the recommendation to look at read.table.> 2. substract to those all values of file 2 (one by one) and store the > result in a new vector > 3. Create a sliding window of chosen size and calculate the mathematical > average and store these averages in a new vector > 4. Plot the different graphes with a bar plotThose will follow simply once you have the first part working. HTH -- ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~. ___ Patrick Connolly {~._.~} Great minds discuss ideas _( Y )_ Average minds discuss events (:_~*~_:) Small minds discuss people (_)-(_) ..... Eleanor Roosevelt ~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.~.
1Rnwb
2009-Jul-31 14:37 UTC
[R] Newbie in R: Reading .txt files and storing the 'numbers' in a vector
I am also a newbie, but here are the few thing i do eo=read.table(file="path where you have saved your text file/xxx.txt",header=T, ) #this will only read excelfiles saved as *.csv file attach(eo) #attach the data names(eo) go to this website http://www.statmethods.net/stats here is how to store numbers as vectors a<-c(1,2,3,4,5,6,7) here is how i means as vector d<-aggregate(LMMP8,list(PlateID,Self_T1D),mean, na.rm=T) I would suggest u to search for website for R tutorials here are some good ones go to this website http://www.statmethods.net/stats http://cran.r-project.org/doc/contrib/Lemon-kickstart/kr_ttest.html this one has all the codes you can change them according to your data http://www3.imperial.ac.uk/pls/portallive/docs/1/1171930.TXT http://www.mayin.org/ajayshah/KB/R/index.html also download the book by verjani it would be a great help to you. Patrick Schorderet wrote:> > > Hello everybody, > > I'm a newbie in R and just went through an introduction class recently. > > Here's my problem. > I have 2 text files (.txt) with plain numbers ('doubles' for those who > know c++) ordered into 2 columns as below: > > coordinate1 value1 > coordinate2 value2 > coordinate3 value3 > ... ... > coordinateN valueN > > I would like to write a small programme in which i would: > 1. take all values of file 1 and store them in a vector > 2. substract to those all values of file 2 (one by one) and store the > result in a new vector > 3. Create a sliding window of chosen size and calculate the > mathematical average and store these averages in a new vector > 4. Plot the different graphes with a bar plot > > So I wrote a c++ script that does the first parts and writes the > averages in a output.txt file (which I have up and running). > Unfortunately, I seem to fail in reading these as numbers and putting > them into a vector (in the R environment). I can read the file , but > this is what I get: > > > z=scan(file = "/Users/NCCRGENETICS/Desktop/testRnumbers.txt", what > = "double") > Read 19 items > > z > [1] "{\\rtf1\\ansi\\ansicpg1252\\cocoartf949\\cocoasubrtf430" > [2] "{\\fonttbl\\f0\\fswiss\\fcharset0" > [3] "Helvetica;}" > [4] "{\\colortbl;\\red255\\green255\\blue255;}" > [5] "\\paperw11900\\paperh16840\\margl1440\\margr1440\\vieww9000\ > \viewh8400\\viewkind0" > [6] "\\pard\\tx566\\tx1133\\tx1700\\tx2267\\tx2834\\tx3401\\tx3968\ > \tx4535\\tx5102\\tx5669\\tx6236\\tx6803\\ql\\qnatural\\pardirnatural" > [7] "\\f0\\fs24" > [8] "\\cf0" > [9] "13\\" // This is where my file "should" start > reading the numbers I see in the window when I open it with textedit > [10] "27\\" > [11] "39\\" > [12] "48\\" > [13] "34\\" > [14] "17\\" > [15] "23\\" > [16] "45\\" > [17] "23\\" > [18] "4\\" > [19] "}" > > > > Thanks so much to any of you who could provoid a little help... I'm > getting nuts. > Patrick > > ______________________________________________ > 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. > >-- View this message in context: http://www.nabble.com/Newbie-in-R%3A-Reading-.txt-files-and-storing-the-%27numbers%27-in-a-vector-tp24714734p24757501.html Sent from the R help mailing list archive at Nabble.com.