Both sides previous revisionPrevious revisionNext revision | Previous revisionLast revisionBoth sides next revision |
r:input_output [2018/11/16 08:09] – [Redirecting Output to a File] hkimscil | r:input_output [2019/09/20 10:29] – [Reading from CSV Files] hkimscil |
---|
cat(conclusion, file="analysisReport.out", append=TRUE)</code> | 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. | 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") |
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> |
| |
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> |
</code> | </code> |
| |
<code>> tbl <- read.csv("filename")</code> | <code>> tbl <- read.csv("filename") |
| > tbl <- read.csv("http://commres.net/wiki/_export/code/r/input_output?codeblock=24") |
| > tbl</code> |
| |
<code>> tbl <- read.csv("filename", header=FALSE)</code> | <code>> tbl <- read.csv("http://commres.net/wiki/_export/code/r/input_output?codeblock=24", header=FALSE) |
| > tbl</code> |
| |
| <code>> tbl <- read.csv("http://commres.net/wiki/_export/code/r/input_output?codeblock=24", header=FALSE, sep="\t") |
| > tbl</code> |
| |
<code>> str(tbl) | <code>> str(tbl) |
> 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> |
| |