The Big R-Book. Philippe J. S. De Brouwer

Чтение книги онлайн.

Читать онлайн книгу The Big R-Book - Philippe J. S. De Brouwer страница 32

The Big R-Book - Philippe J. S. De Brouwer

Скачать книгу

frames are the prototype of all two-dimensional data (also known as “rectangular data”). For statistical analysis this is obviously an important data-type.

       data frame

       rectangular data

      Data frames are very useful for statistical modelling; they are objects that contain data in a tabular way. Unlike a matrix in data frame each column can contain different types of data. For example, the first column can be factorial, the second logical, and the third numerical. It is a composite data type consisting of a list of vectors of equal length.

      Data frames are created using the data.frame() function.

       data.frame()

       pairs()

Schematic illustration of the standard plot for a data frame in R shows each column printed in function of each other. This is useful to see correlations or how generally the data is structured.

      4.3.8.2 Accessing Information from a Data Frame

       summary()

       head()

       tail()

      # Get the structure of the data frame: str(data_test) ## ‘data.frame’: 5 obs. of 4 variables: ## $ Name : Factor w/ 5 levels “Laura”,“Lisa”,..: 5 4 3 2 1 ## $ Gender: Factor w/ 2 levels “Female”,“Male”: 2 2 1 1 1 ## $ Score : num 78 88 92 89 84 ## $ Age : num 42 38 26 30 35 # Note that the names became factors (see warning below) # Get the summary of the data frame: summary(data_test) ## Name Gender Score Age ## Laura:1 Female:3 Min. :78.0 Min. :26.0 ## Lisa :1 Male :2 1st Qu.:84.0 1st Qu.:30.0 ## Paula:1 Median :88.0 Median :35.0 ## Pawel:1 Mean :86.2 Mean :34.2 ## Piotr:1 3rd Qu. :89.0 3rd Qu.:38.0 ## Max. :92.0 Max. :42.0 # Get the first rows: head(data_test) ## Name Gender Score Age ## 1 Piotr Male 78 42 ## 2 Pawel Male 88 38 ## 3 Paula Female 92 26 ## 4 Lisa Female 89 30 ## 5 Laura Female 84 35 # Get the last rows: tail(data_test) ## Name Gender Score Age ## 1 Piotr Male 78 42 ## 2 Pawel Male 88 38 ## 3 Paula Female 92 26 ## 4 Lisa Female 89 30 ## 5 Laura Female 84 35 # Extract the column 2 and 4 and keep all rows data_test.1 <- data_test[,c(2,4)] print(data_test.1) ## Gender Age ## 1 Male 42 ## 2 Male 38 ## 3 Female 26 ## 4 Female 30 ## 5 Female 35 # Extract columns by name and keep only selected rows data_test[c(2:4),c(2,4)] ## Gender Age ## 2 Male 38 ## 3 Female 26 ## 4 Female 30

      image Warning – Avoiding conversion to factors

      d <- data.frame( Name = c(“Piotr”, “Pawel”,“Paula”,“Lisa”,“Laura”), Gender = c(“Male”, “Male”,“Female”, “Female”,“Female”), Score = c(78,88,92,89,84), Age = c(42,38,26,30,35), stringsAsFactors = FALSE ) d$Gender <- factor(d$Gender) # manually factorize gender str(d) ## ‘data.frame’: 5 obs. of 4 variables: ## $ Name : chr “Piotr” “Pawel” “Paula” “Lisa” … ## $ Gender: Factor w/ 2 levels “Female”,“Male”: 2 2 1 1 1 ## $ Score : num 78 88 92 89 84 ## $ Age : num 42 38 26 30 35

      4.3.8.3 Editing Data in a Data Frame

      While one usually reads in large amounts of data and uses an IDE such as RStudio that facilitates the visualization and manual modification of data frames, it is useful to know how this is done when no graphical interface is available. Even when working on a server, all these functions will always be available.

       de()

       data.entry()

       edit()

      de(x) # fails if x is not defined de(x <- c(NA)) # works x <- de(x <- c(NA)) # will also save the changes data.entry(x) # de is short for data.entry x <- edit(x) # use the standard editor (vi in *nix)

      Of course, there are also multiple ways to address data directly in R.

      # The following lines do the same. data_test$Score[1] <- 80 data_test[3,1] <- 80

      4.3.8.4 Modifying Data Frames

      Add Columns to a Data-frame

      Typically, the variables are in the columns and adding a column corresponds to adding a new, observed variable. This is done via the function cbind().

       cbind()

Скачать книгу