Section 3 Dataframes and More

3.1 Dataframes!

Data frames are used to store and analyse multiple rows and columns of data bundled together in a table. They are essentially what we have looked at above.

They are the most common format for working with data is in a table, with data arranged in rows and columns.

Most of the time, you’ll read your data from a file (a spreadsheet, an SPSS file, etc.) and it will be read in as a dataframe. For example, data is a data frame

class(data)
## [1] "tbl_df"     "tbl"        "data.frame"

3.1.1 Accessing parts of dataframes

As you get used to R, you should find that you get more comfortable using the console to check on your data. This is a reasonably big dataset, with 1421 rows (visible in the “Environment” pane in RStudio. If you just print the data in the console by entering data, you’ll get a lot of output (but luckily not all 1000+ rows). Instead, it’s often best to use head(data) to see the first few rows when you just want to look at the basic format of your data. You can often see a lot of the information you need by printing the first few rows of a dataframe to the console.

The head() function prints the first 6 rows of a table by default, and you can select the columns that are most relevant to what you’re working on if there are too many:

head(data)
## # A tibble: 6 x 5
##   neuroticism extraversion sex    volunteer treatment
##         <dbl>        <dbl> <chr>  <chr>         <dbl>
## 1          16           13 female no                1
## 2           8           14 male   no                2
## 3           5           16 male   no                2
## 4           8           20 female no                1
## 5           9           19 male   no                2
## 6           6           15 male   no                2
head(data[, c("sex", "volunteer")])
## # A tibble: 6 x 2
##   sex    volunteer
##   <chr>  <chr>    
## 1 female no       
## 2 male   no       
## 3 male   no       
## 4 female no       
## 5 male   no       
## 6 male   no

Accessing a single column

To access a single column from a dataframe, you can use $, which will return a single vector:

data$sex
##    [1] "female" "male"   "male"   "female" "male"   "male"   "female" "male"   "male"   "male"   "female" "male"  
##   [13] "male"   "male"   "female" "male"   "male"   "male"   "male"   "female" "male"   "female" "male"   "female"
##   [25] "female" "female" "male"   "female" "female" "male"   "male"   "female" "female" "male"   "female" "male"  
##   [37] "female" "female" "female" "female" "male"   "female" "female" "male"   "female" "male"   "male"   "female"
##   [49] "male"   "female" "male"   "female" "female" "female" "female" "female" "female" "female" "female" "female"
##   [61] "male"   "female" "male"   "female" "female" "female" "female" "male"   "male"   "male"   "male"   "female"
##   [73] "female" "female" "female" "female" "male"   "female" "female" "male"   "male"   "male"   "male"   "female"
##   [85] "female" "female" "female" "female" "female" "female" "female" "female" "male"   "female" "female" "female"
##   [97] "male"   "female" "female" "male"   "female" "male"   "male"   "male"   "male"   "male"   "male"   "female"
##  [109] "female" "male"   "female" "female" "female" "female" "male"   "male"   "male"   "male"   "male"   "male"  
##  [121] "male"   "female" "male"   "female" "female" "female" "female" "male"   "male"   "female" "female" "male"  
##  [133] "male"   "female" "female" "male"   "male"   "male"   "female" "female" "male"   "female" "female" "male"  
##  [145] "male"   "male"   "male"   "female" "female" "female" "female" "male"   "male"   "male"   "female" "male"  
##  [157] "female" "female" "female" "female" "male"   "male"   "male"   "female" "male"   "male"   "female" "male"  
##  [169] "male"   "female" "male"   "female" "male"   "female" "female" "male"   "female" "male"   "female" "male"  
##  [181] "male"   "male"   "female" "male"   "female" "female" "male"   "female" "male"   "male"   "male"   "male"  
##  [193] "male"   "male"   "male"   "female" "female" "male"   "male"   "female" "male"   "female" "female" "female"
##  [205] "male"   "female" "female" "male"   "male"   "female" "male"   "female" "male"   "male"   "female" "female"
##  [217] "male"   "female" "female" "male"   "female" "female" "female" "female" "male"   "female" "male"   "male"  
##  [229] "male"   "female" "male"   "male"   "female" "male"   "male"   "male"   "male"   "male"   "male"   "male"  
##  [241] "male"   "male"   "female" "male"   "male"   "male"   "male"   "male"   "male"   "female" "male"   "female"
##  [253] "male"   "male"   "male"   "male"   "male"   "female" "female" "female" "male"   "male"   "male"   "male"  
##  [265] "male"   "male"   "male"   "female" "female" "female" "male"   "male"   "male"   "male"   "male"   "male"  
##  [277] "female" "male"   "female" "male"   "female" "female" "female" "female" "female" "male"   "female" "male"  
##  [289] "male"   "female" "female" "female" "male"   "male"   "male"   "male"   "male"   "male"   "male"   "male"  
##  [301] "female" "female" "male"   "female" "female" "male"   "female" "male"   "male"   "female" "male"   "male"  
##  [313] "male"   "male"   "male"   "female" "female" "female" "male"   "male"   "male"   "female" "female" "male"  
##  [325] "male"   "female" "female" "male"   "female" "male"   "male"   "female" "male"   "female" "male"   "female"
##  [337] "male"   "female" "male"   "female" "female" "female" "female" "male"   "female" "female" "female" "female"
##  [349] "male"   "male"   "male"   "male"   "female" "female" "male"   "female" "male"   "male"   "male"   "male"  
##  [361] "male"   "male"   "male"   "female" "female" "female" "male"   "male"   "male"   "male"   "female" "male"  
##  [373] "male"   "male"   "male"   "female" "female" "female" "female" "female" "male"   "female" "male"   "male"  
##  [385] "male"   "female" "male"   "female" "male"   "female" "male"   "female" "female" "male"   "female" "female"
##  [397] "male"   "male"   "female" "male"   "female" "male"   "male"   "male"   "male"   "male"   "male"   "male"  
##  [409] "female" "female" "male"   "male"   "female" "female" "male"   "female" "female" "female" "female" "male"  
##  [421] "female" "male"   "female" "male"   "female" "female" "female" "female" "female" "female" "male"   "male"  
##  [433] "male"   "male"   "female" "female" "female" "female" "male"   "male"   "female" "male"   "female" "female"
##  [445] "male"   "male"   "female" "female" "male"   "male"   "male"   "male"   "male"   "female" "female" "female"
##  [457] "female" "female" "male"   "male"   "female" "male"   "female" "female" "male"   "female" "male"   "female"
##  [469] "female" "male"   "male"   "male"   "male"   "male"   "male"   "male"   "male"   "female" "male"   "female"
##  [481] "male"   "male"   "male"   "male"   "female" "male"   "female" "female" "male"   "male"   "female" "male"  
##  [493] "female" "male"   "female" "male"   "male"   "female" "female" "female" "female" "female" "female" "male"  
##  [505] "male"   "male"   "female" "female" "male"   "female" "male"   "female" "female" "male"   "female" "male"  
##  [517] "male"   "male"   "female" "male"   "female" "female" "female" "female" "male"   "female" "male"   "female"
##  [529] "male"   "male"   "female" "female" "female" "female" "female" "female" "male"   "female" "female" "male"  
##  [541] "female" "female" "female" "female" "female" "female" "male"   "female" "female" "female" "male"   "female"
##  [553] "female" "male"   "male"   "female" "female" "female" "female" "female" "female" "female" "female" "female"
##  [565] "male"   "female" "female" "male"   "female" "female" "female" "male"   "male"   "female" "male"   "female"
##  [577] "female" "female" "female" "male"   "male"   "female" "male"   "female" "female" "female" "female" "female"
##  [589] "male"   "female" "female" "male"   "female" "male"   "female" "male"   "female" "male"   "female" "male"  
##  [601] "female" "female" "female" "female" "male"   "female" "male"   "female" "male"   "female" "male"   "female"
##  [613] "male"   "male"   "male"   "male"   "male"   "male"   "female" "male"   "female" "male"   "male"   "female"
##  [625] "male"   "female" "female" "male"   "female" "female" "male"   "male"   "female" "female" "male"   "female"
##  [637] "male"   "male"   "male"   "male"   "female" "female" "female" "female" "female" "female" "female" "female"
##  [649] "female" "female" "female" "female" "female" "female" "male"   "female" "female" "female" "female" "male"  
##  [661] "male"   "male"   "male"   "male"   "female" "male"   "female" "female" "female" "female" "female" "female"
##  [673] "female" "female" "male"   "female" "female" "female" "female" "female" "female" "female" "female" "female"
##  [685] "male"   "female" "female" "female" "female" "female" "female" "female" "male"   "female" "female" "male"  
##  [697] "male"   "male"   "female" "male"   "female" "male"   "female" "female" "male"   "male"   "male"   "male"  
##  [709] "female" "male"   "female" "male"   "female" "female" "female" "male"   "male"   "female" "female" "female"
##  [721] "male"   "female" "female" "male"   "male"   "female" "female" "female" "female" "female" "female" "female"
##  [733] "female" "female" "female" "female" "male"   "female" "female" "female" "female" "female" "female" "female"
##  [745] "female" "female" "female" "female" "female" "female" "female" "female" "female" "male"   "female" "male"  
##  [757] "female" "female" "female" "male"   "female" "female" "female" "female" "female" "female" "female" "male"  
##  [769] "female" "female" "female" "female" "female" "female" "male"   "male"   "male"   "male"   "female" "male"  
##  [781] "female" "male"   "male"   "male"   "male"   "female" "female" "male"   "male"   "male"   "male"   "male"  
##  [793] "female" "female" "female" "female" "female" "male"   "female" "female" "female" "female" "male"   "male"  
##  [805] "female" "male"   "female" "female" "female" "female" "female" "female" "female" "female" "female" "female"
##  [817] "female" "female" "female" "male"   "female" "female" "female" "male"   "female" "male"   "female" "male"  
##  [829] "female" "female" "female" "male"   "male"   "male"   "male"   "female" "female" "female" "female" "male"  
##  [841] "female" "female" "female" "male"   "female" "male"   "male"   "male"   "male"   "male"   "male"   "female"
##  [853] "female" "male"   "male"   "female" "male"   "female" "female" "female" "female" "female" "male"   "female"
##  [865] "female" "male"   "male"   "female" "male"   "female" "female" "female" "female" "female" "female" "female"
##  [877] "female" "male"   "male"   "male"   "male"   "female" "male"   "male"   "female" "female" "female" "female"
##  [889] "male"   "male"   "male"   "male"   "female" "female" "female" "female" "female" "female" "female" "female"
##  [901] "female" "female" "male"   "female" "female" "female" "male"   "female" "female" "female" "female" "male"  
##  [913] "female" "female" "male"   "male"   "male"   "female" "female" "female" "female" "male"   "male"   "male"  
##  [925] "male"   "female" "male"   "female" "male"   "female" "female" "male"   "male"   "male"   "male"   "male"  
##  [937] "female" "male"   "male"   "female" "female" "male"   "female" "female" "male"   "female" "female" "female"
##  [949] "male"   "male"   "male"   "female" "female" "female" "female" "female" "female" "female" "female" "female"
##  [961] "male"   "male"   "female" "female" "female" "female" "male"   "female" "male"   "female" "male"   "female"
##  [973] "female" "male"   "female" "male"   "female" "male"   "female" "male"   "male"   "male"   "male"   "female"
##  [985] "male"   "female" "male"   "male"   "female" "female" "female" "male"   "female" "female" "male"   "male"  
##  [997] "male"   "male"   "female" "male"  
##  [ reached getOption("max.print") -- omitted 421 entries ]
## attr(,"format.spss")
## [1] "A6"
## attr(,"display_width")
## [1] 6

I actually prefer to use a function from the tidyverse package called “select,” which allows you to select 1 or more variables that you want to view from your dataframe.

temp <- dplyr::select(data, sex, volunteer)

Data viewer example

Data viewer example