User Tools

Site Tools


r:input_output

Differences

This shows you the differences between two versions of the page.

Link to this comparison view

Both sides previous revisionPrevious revision
Next revision
Previous revision
r:input_output [2017/09/28 08:05] hkimscilr:input_output [2019/09/20 10:32] (current) – [Reading from CSV Files] hkimscil
Line 38: Line 38:
 > source("script.R"                  # Run the script, capturing its output > source("script.R"                  # Run the script, capturing its output
 > sink()                               # Resume writing output to console</code> > sink()                               # Resume writing output to console</code>
 +
 +<code>cat(data, file="analysisReport.out")
 +cat(results, file="analysisRepart.out", append=TRUE)
 +cat(conclusion, file="analysisReport.out", append=TRUE)</code>
 +
 +Without "append=TRUE" option, the content will be **overwritten** with the succeeding content. The above method is tedious and error prone. The second line has a different filename. The below method (making connection to file with file function) is better. 
  
 <code>con <- file("analysisReport.out", "w") <code>con <- file("analysisReport.out", "w")
Line 45: Line 51:
 close(con)</code> close(con)</code>
  
 +"file("filename.txt", "w")" opens "filename.txt and remains open until "close(opened_variable)" function. In this case, append=TRUE option is not necessary.
 ====== Listing Files ====== ====== Listing Files ======
 <code>list.files()</code> <code>list.files()</code>
Line 56: Line 63:
 <code>> records <- read.fwf("filename", widths=c(w1, w2, ..., wn))</code> <code>> records <- read.fwf("filename", widths=c(w1, w2, ..., wn))</code>
  
-<code>Fisher    R.A.      1890 1962+<code txt fixed-width.txt> 
 +Fisher    R.A.      1890 1962
 Pearson   Karl      1857 1936 Pearson   Karl      1857 1936
 Cox       Gertrude  1900 1978 Cox       Gertrude  1900 1978
 Yates     Frank     1902 1994 Yates     Frank     1902 1994
 Smith     Kirstine  1878 1939 Smith     Kirstine  1878 1939
 +
 </code> </code>
  
-<code>> records <- read.fwf("fixed-width.txt", widths=c(10,10,4,-1,4))+<code>> records <- read.fwf("http://commres.net/wiki/_export/code/r/input_output?codeblock=11", widths=c(10,10,4,-1,4))
 </code> </code>
  
Line 76: Line 85:
 5 Smith      Kirstine   1878 1939 5 Smith      Kirstine   1878 1939
 </code> </code>
 +Funky variable names (V1, V2, . . .). Use the below method with "col.names=c()" option.
  
 +<code>> records <- read.fwf("http://commres.net/wiki/_export/code/r/input_output?codeblock=11", widths=c(10,10,4,-1,4),
 ++                     col.names=c("Last","First","Born","Died"))
 +> records
 +        Last      First Born Died
 +1 Fisher     R.A.       1890 1962
 +2 Pearson    Karl       1857 1936
 +3 Cox        Gertrude   1900 1978
 +4 Yates      Frank      1902 1994
 +5 Smith      Kirstine   1878 1939
 +</code>
 ====== Reading Tabular Data Files ====== ====== Reading Tabular Data Files ======
 <code>> dfrm <- read.table("filename")</code> <code>> dfrm <- read.table("filename")</code>
Line 86: Line 106:
 Yates Frank 1902 1994 Yates Frank 1902 1994
 Smith Kirstine 1878 1939 Smith Kirstine 1878 1939
-.+
  
 </code> </code>
Line 139: Line 159:
  
 ====== Reading from CSV Files ====== ====== Reading from CSV Files ======
-<code>> tbl <- read.csv("filename")</code>+<code csv filename.csv>label lbound ubound 
 +low 0.000 0.674 
 +mid 0.674 1.640 
 +high 1.640 2.330 
 +</code> 
 + 
 +<code>> tbl <- read.csv("filename.csv") 
 +> tbl</code> 
 + 
 +<code>> tbl <- read.csv("filename.csv", header=FALSE) 
 +> tbl</code>
  
-<code>> tbl <- read.csv("filename", header=FALSE)</code>+<code>> tbl <- read.csv("filename.csv", header=FALSE, sep="\t") 
 +> tbl</code>
  
 <code>> str(tbl) <code>> str(tbl)
Line 199: Line 230:
 > url <- 'http://en.wikipedia.org/wiki/World_population' > url <- 'http://en.wikipedia.org/wiki/World_population'
 > tbls <- readHTMLTable(url) > tbls <- readHTMLTable(url)
 +</code>
 +
 +The above not working for me. :(
 +
 +<code>library(httr)
 +url <- 'http://en.wikipedia.org/wiki/World_population'
 +tables <- GET(url)
 +tables <- readHTMLTable(rawToChar(tables$content))
 </code> </code>
  
r/input_output.1506555318.txt.gz · Last modified: 2017/09/28 08:05 by hkimscil

Donate Powered by PHP Valid HTML5 Valid CSS Driven by DokuWiki