User Tools

Site Tools


en:learning:schools:s01:lecture-notes:ba-ln-02

Differences

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

Link to this comparison view

en:learning:schools:s01:lecture-notes:ba-ln-02 [2015/09/22 16:22]
en:learning:schools:s01:lecture-notes:ba-ln-02 [2015/09/22 16:22] (current)
Line 1: Line 1:
 +====== L02: Tabulated data I/O ======
 +
 +"Clu, we don't have much time left to find that file. This is top priority."​
 +
 +Kevin Flynn, Tron
 +
 +==== Things we cover in this session ====
 +  * Reading tabulated data sets
 +  * Accessing data values in data frames
 +
 +==== Things you need for this session ====
 +  * [[en:​learning:​schools:​s01:​worksheets:​ba-ws-02-1|W02-1:​ Reading CSV files]]
 +
 +==== Things to take home from this session ====
 +At the end of this session you should be able to
 +  * read data from CSV files
 +  * access individual cells, entire columns or rows of data frames
 +
 +===== CSV I/O =====
 +In general, reading or writing from/to structured files with R is quite simple and straight forward. While other programing languages often require the explicit opening and closing of a file, the respective R functions called ''​read.table''​ and ''​write.table''​ are an all-round carefree package. As a result, these functions return a variable of type data frame which you can think of as a table in Excel or any other spreadsheet software.
 +
 +8-O Have a look at [[en:​learning:​schools:​s01:​code-examples:​ba-ce-02-01|C02-1 CSV I/O]] now for a hands-on introduction. ​
 +
 +For more information,​ you can also search the web, refer to Quick-R'​s [[http://​www.statmethods.net/​input/​importingdata.html|importing]] and [[http://​www.statmethods.net/​input/​exportingdata.html|exporting]] data section or type ''?​read.table''​ or ''?​write.table''​ inside your R environment.
 +
 +<note tip>R has a variety of packages for reading and writing specific data sets. Before you start programming your own I/O routine, search the internet like "R reading XY files"</​note>​
 +
 +===== Accessing or modifying values in data frames =====
 +You already have noticed that when reading tabulated data into R, the variable in which the data is stored is of type data frame. In turn, each column of the data frame is of a certain data type.
 +
 +To access individual values in R data structures you have to specify the section inside the respective variable where the desired value(s) are stored. This sounds much more complicated than it is.
 +
 +Imagine a table with a single row. Such a row would be a graphical representation of a vector in R. If you are interested into the third value, you will have a look into the third column. The same is true for R: you access the third value of a vector v by selecting it within square brackets like ''​v[3]''​. For other data structures the same logic applies, also it might be necessary to use double brackets combined with single ones (e.g. ''​l[[1]][3]''​ gives you the third value in the simple list l) or supply more than one dimension (e.g. ''​df[3,​2]''​ gives you the value of the third row and second column inside the data frame df).
 +
 +8-O Have a look at [[en:​learning:​schools:​s01:​code-examples:​ba-ce-00-02|C00-2 Data frame basics]] now for a hands-on introduction for data frames. ​
 +
 +In [[en:​learning:​schools:​s01:​code-examples:​ba-ce-00-01|C00-1 Vector basics]] you find the corresponding explanations for vectors if you need them.
 +
 +m( Introducing R data types and structures in more detail is beyond the scope of this school. For some more information on that topic, please refer to the excursus [[en:​learning:​schools:​s01:​excursus:​ba-ex-02-01|E02-1:​ Data types and structures]].
 +===== Time for practice =====
 +
 +[[en:​learning:​schools:​s01:​worksheets:​ba-ws-02-1|W02-1:​ Reading CSV files]]
 +
  
en/learning/schools/s01/lecture-notes/ba-ln-02.txt ยท Last modified: 2015/09/22 16:22 (external edit)