R - Lists

Introduction

Lists are the objects which contain elements of different types like numbers, strings, vectors, data frames and another list inside it. A list can also contain a matrix or even function as its elements. List is created using list() function. Lists are typically used for organizing data rather than processing it.

Creating a List

Lists are created using the list() function. Following is an example to create a list containing strings, numbers, vectors and a logical values.
# Create a list containing strings, numbers, vectors and a logical
# values.
> list_data <- list(mtcars[1:5,], c('A', 'Sample', 'Vector'), c(21,32,11), TRUE, 51.23, 119.1)
> list_data
[[1]]
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

[[2]]
[1] "A"      "Sample" "Vector"

[[3]]
[1] 21 32 11

[[4]]
[1] TRUE

[[5]]
[1] 51.23

[[6]]
[1] 119.1

>
As you can see, each item in the list is associated with an index number that is shown as [[1]], [[2]]. We can also assign names to these elements.

Naming List Elements

The list elements can be given names and they can be accessed using these names.
> # Create a list containing a vector, a matrix and a list.
> list_data <- list(df = mtcars[1:5,], vec1 = c('A', 'Sample', 'Vector'), vec2 = c(21,32,11), bln = TRUE, num1 = 51.23, num2 = 119.1)
> list_data
$df
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

$vec1
[1] "A"      "Sample" "Vector"

$vec2
[1] 21 32 11

$bln
[1] TRUE

$num1
[1] 51.23

$num2
[1] 119.1

>
You can check for the names in a list using
> names(list_data)
[1] "df"   "vec1" "vec2" "bln"  "num1" "num2"
Now we can also assign names to these objects
> names(list_data) <- c("Data Frame", "Vector 1", "Vector 2", "Boolean", "Number 1", "Number 2")
>
This updates the names of the list elements
> list_data
$`Data Frame`
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

$`Vector 1`
[1] "A"      "Sample" "Vector"

$`Vector 2`
[1] 21 32 11

$Boolean
[1] TRUE

$`Number 1`
[1] 51.23

$`Number 2`
[1] 119.1

>

Accessing List Elements

Elements of the list can be accessed by the index of the element in the list. In case of named lists it can also be accessed using the names.
> list_data[1]
$`Data Frame`
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

> 

> list_data[1]
$`Data Frame`
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

> 
> list_data$`Vector 1`
[1] "A"      "Sample" "Vector"
>

Manipulating List Elements

We can add, delete and update list elements as shown below. We can add only at the end of a list. But we can update/delete any element.
> list_data[4] <- NULL
> list_data
$`Data Frame`
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2

$`Vector 1`
[1] "A"      "Sample" "Vector"

$`Vector 2`
[1] 21 32 11

$`Number 1`
[1] 51.23

$`Number 2`
[1] 119.1

>

Merging Lists

You can merge many lists into one list by placing all the lists inside one list() function.
# Create two lists.
list1 <- list(1,2,3)
list2 <- list("Sun","Mon","Tue")

# Merge the two lists.
merged.list <- c(list1,list2)

# Print the merged list.
merged.list

[[1]]
[1] 1

[[2]]
[1] 2

[[3]]
[1] 3

[[4]]
[1] "Sun"

[[5]]
[1] "Mon"

[[6]]
[1] "Tue"

Converting List to Vector

A list can be converted to a vector so that the elements of the vector can be used for further manipulation. All the arithmetic operations on vectors can be applied after the list is converted into vectors. To do this conversion, we use the unlist() function. It takes the list as input and produces a vector.
> list1 <- list(1:5)
> list1
[[1]]
[1] 1 2 3 4 5

> 
> list2 <-list(10:14)
> list2
[[1]]
[1] 10 11 12 13 14

> 
> v1 <- unlist(list1)
> v2 <- unlist(list2)
> 
> v1
[1] 1 2 3 4 5
> v2
[1] 10 11 12 13 14
> 
> result <- v1+v2
> result
[1] 11 13 15 17 19
>
As mentioned before, Lists are mainly used for organizing data and not so much for processing. They provide an excellent tool for organizing data. This is a very basic introduction to processing R Lists. There is a lot more to the language and you can check out the various References for more details.

R - Data Frames

Introduction

All data in vectors and matrices is enforced to a single data type. But Data Frames let you overcome this limitation. A data frame can contain several elements of different types. An example of R Data Frame can be seen here:
> WorldPhones
     N.Amer Europe Asia S.Amer Oceania Africa Mid.Amer
1951  45939  21574 2876   1815    1646     89      555
1956  60423  29990 4708   2568    2366   1411      733
1957  64721  32510 5230   2695    2526   1546      773
1958  68484  35218 6662   2845    2691   1663      836
1959  71799  37598 6856   3000    2868   1769      911
1960  76036  40341 8220   3145    3054   1905     1008
1961  79831  43173 9053   3338    3224   2005     1076
>

Creating Data Frame

A new data frame object can be created using the function data.frame()
> empty <- data.frame() # empty data frame
> vector.1 <- 1:10 # vector of integers
> vector.2 <- letters[1:10] # vector of strings
> df <- data.frame(column.1=vector.1,column.2=vector.2)
> 
> df
   column.1 column.2
1         1        a
2         2        b
3         3        c
4         4        d
5         5        e
6         6        f
7         7        g
8         8        h
9         9        i
10       10        j
>

Importing and Exporting Data

You can export and import the data frame to a CSV file. This is useful for saving the context of a data operation.
> write.csv(df, file='mydata.csv')     # Save the data frame to CSV file
>
You can load the contents from the CSV file as below
> d2 <- read.csv('mydata.csv')         # Load the data frame from CSV file
> d2
    X column.1 column.2
1   1        1        a
2   2        2        b
3   3        3        c
4   4        4        d
5   5        5        e
6   6        6        f
7   7        7        g
8   8        8        h
9   9        9        i
10 10       10        j
Please note that there is a difference in what we saved and what we read from the file. The row numbers are also saved in the CSV and then loaded as an independent column when reading from the CSV.

Analyzing the Data Frame

While analyzing the data, it is very useful if we can have an initial idea about the kind of data present in the data frame - the columns, the data type, max/min/mean values for numbers, etc. R provides a good set of utilities to make this job simpler. Let us try to understand the data frame states.x77

Head / Tail

The data frame is too big to be viewed manually. We can get a very basic glimpse of the data in there by using the head.
> head(state.x77)
           Population Income Illiteracy Life Exp Murder HS Grad Frost   Area
Alabama          3615   3624        2.1    69.05   15.1    41.3    20  50708
Alaska            365   6315        1.5    69.31   11.3    66.7   152 566432
Arizona          2212   4530        1.8    70.55    7.8    58.1    15 113417
Arkansas         2110   3378        1.9    70.66   10.1    39.9    65  51945
California      21198   5114        1.1    71.71   10.3    62.6    20 156361
Colorado         2541   4884        0.7    72.06    6.8    63.9   166 103766
>
Or you can use tail to get the last 6 elements
> tail(state.x77)
              Population Income Illiteracy Life Exp Murder HS Grad Frost  Area
Vermont              472   3907        0.6    71.64    5.5    57.1   168  9267
Virginia            4981   4701        1.4    70.08    9.5    47.8    85 39780
Washington          3559   4864        0.6    71.72    4.3    63.5    32 66570
West Virginia       1799   3617        1.4    69.48    6.7    41.6   100 24070
Wisconsin           4589   4468        0.7    72.48    3.0    54.5   149 54464
Wyoming              376   4566        0.6    70.29    6.9    62.9   173 97203
>
Please note that 6 is just the default value for number of rows in head and tail. You can always override it using the second parameter
> head(mtcars, 3)
               mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4     21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag 21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710    22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
> 
> tail(mtcars, 3)
               mpg cyl disp  hp drat   wt qsec vs am gear carb
Ferrari Dino  19.7   6  145 175 3.62 2.77 15.5  0  1    5    6
Maserati Bora 15.0   8  301 335 3.54 3.57 14.6  0  1    5    8
Volvo 142E    21.4   4  121 109 4.11 2.78 18.6  1  1    4    2
>

Summary and Structure

R also provides two more utility functions that help you understand the data
> #Structure
> str(state.x77)
 num [1:50, 1:8] 3615 365 2212 2110 21198 ...
 - attr(*, "dimnames")=List of 2
  ..$ : chr [1:50] "Alabama" "Alaska" "Arizona" "Arkansas" ...
  ..$ : chr [1:8] "Population" "Income" "Illiteracy" "Life Exp" ...
> 
> summary(state.x77)
   Population        Income       Illiteracy       Life Exp         Murder          HS Grad          Frost             Area       
 Min.   :  365   Min.   :3098   Min.   :0.500   Min.   :67.96   Min.   : 1.400   Min.   :37.80   Min.   :  0.00   Min.   :  1049  
 1st Qu.: 1080   1st Qu.:3993   1st Qu.:0.625   1st Qu.:70.12   1st Qu.: 4.350   1st Qu.:48.05   1st Qu.: 66.25   1st Qu.: 36985  
 Median : 2838   Median :4519   Median :0.950   Median :70.67   Median : 6.850   Median :53.25   Median :114.50   Median : 54277  
 Mean   : 4246   Mean   :4436   Mean   :1.170   Mean   :70.88   Mean   : 7.378   Mean   :53.11   Mean   :104.46   Mean   : 70736  
 3rd Qu.: 4968   3rd Qu.:4814   3rd Qu.:1.575   3rd Qu.:71.89   3rd Qu.:10.675   3rd Qu.:59.15   3rd Qu.:139.75   3rd Qu.: 81163  
 Max.   :21198   Max.   :6315   Max.   :2.800   Max.   :73.60   Max.   :15.100   Max.   :67.30   Max.   :188.00   Max.   :566432  
>

Counts

There is another set of functions that help us understand the meaning of the information contained in the data frame
> ncol(df)
[1] 8
> nrow(df)
[1] 50
> 
> colnames(df)
[1] "Population" "Income"     "Illiteracy" "Life Exp"   "Murder"     "HS Grad"    "Frost"      "Area"      
> rownames(df)
 [1] "Alabama"        "Alaska"         "Arizona"        "Arkansas"       "California"     "Colorado"       "Connecticut"    "Delaware"       "Florida"       
[10] "Georgia"        "Hawaii"         "Idaho"          "Illinois"       "Indiana"        "Iowa"           "Kansas"         "Kentucky"       "Louisiana"     
[19] "Maine"          "Maryland"       "Massachusetts"  "Michigan"       "Minnesota"      "Mississippi"    "Missouri"       "Montana"        "Nebraska"      
[28] "Nevada"         "New Hampshire"  "New Jersey"     "New Mexico"     "New York"       "North Carolina" "North Dakota"   "Ohio"           "Oklahoma"      
[37] "Oregon"         "Pennsylvania"   "Rhode Island"   "South Carolina" "South Dakota"   "Tennessee"      "Texas"          "Utah"           "Vermont"       
[46] "Virginia"       "Washington"     "West Virginia"  "Wisconsin"      "Wyoming"       
>

Referencing Data

Data is useful only if we can access it! R data frames provide several ways of accessing data by columns / rows / individual cells /...

Columns

Let us start with accessing the data in columns using the cars data set as an example
> cars <- mtcars
> head(cars)
                   mpg cyl disp  hp drat    wt  qsec vs am gear carb
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46  0  1    4    4
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02  0  1    4    4
Datsun 710        22.8   4  108  93 3.85 2.320 18.61  1  1    4    1
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44  1  0    3    1
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02  0  0    3    2
Valiant           18.1   6  225 105 2.76 3.460 20.22  1  0    3    1
>
There are many ways of accessing a given column. Each has its own peculiarity.
1. The $ sign is most frequently used to access columns by name. The R Studio also provides you with auto complete for the column names while accessing using the $ sign.
> colv1 <- cars$mpg # returns a vector
> colv1
 [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0
[32] 21.4
>
You can also fetch the column by indexing into the data frame using the column name. This has an advantage that you can fetch multiple columns using a vector of multiple column names.
> colv2 <- cars[, 'mpg'] # returns vector
> colv2
 [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0
[32] 21.4
>
Another commonly used method is to index into the data frame using the column index. This is the added advantage that you can fetch a range of columns
> colv3<- cars[, 1] # returns a vector
> colv3
 [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0
[32] 21.4
>
You can also access the column using the double bracket [[ ]] instead of the single bracket. This does not require the row index. There are subtle differences that you will see later.
> colv4 <- cars[['mpg']] # returns a vector
> colv4
 [1] 21.0 21.0 22.8 21.4 18.7 18.1 14.3 24.4 22.8 19.2 17.8 16.4 17.3 15.2 10.4 10.4 14.7 32.4 30.4 33.9 21.5 15.5 15.2 13.3 19.2 27.3 26.0 30.4 15.8 19.7 15.0
[32] 21.4
>
All of these return a vector. But if you want a data frame instead, you can try something like this:
> # Ways of Returning Data Frames
> mpgdf <- cars['mpg'] # returns 1 col df
> head(mpgdf)
                   mpg
Mazda RX4         21.0
Mazda RX4 Wag     21.0
Datsun 710        22.8
Hornet 4 Drive    21.4
Hornet Sportabout 18.7
Valiant           18.1
>
Or you can also also use numeric index
> mpgdf2 <- cars[1] # returns 1 col df
> head(mpgdf2)
                   mpg
Mazda RX4         21.0
Mazda RX4 Wag     21.0
Datsun 710        22.8
Hornet 4 Drive    21.4
Hornet Sportabout 18.7
Valiant           18.1
>
Here, the outcome is a data frame instead of a vector.

Rows

You can also reference the data using rows. Lets continue using the cars data set to understand this
> df <- cars[1,]
> df
          mpg cyl disp  hp drat   wt  qsec vs am gear carb
Mazda RX4  21   6  160 110  3.9 2.62 16.46  0  1    4    4
>
Or you can use the row name to fetch the row data
> cars['Mazda RX4',]
          mpg cyl disp  hp drat   wt  qsec vs am gear carb
Mazda RX4  21   6  160 110  3.9 2.62 16.46  0  1    4    4
>

Cells

Finally, we can also access the individual cells.
> df <- cars
> df[5,2]
[1] 8
> df[[5,2]]
[1] 8
You can also access a range of cells by similarly indexing into the data frame
> df[1:5,1:5]
                   mpg cyl disp  hp drat
Mazda RX4         21.0   6  160 110 3.90
Mazda RX4 Wag     21.0   6  160 110 3.90
Datsun 710        22.8   4  108  93 3.85
Hornet 4 Drive    21.4   6  258 110 3.08
Hornet Sportabout 18.7   8  360 175 3.15
>
Or you can access the cells of a row
> df['Mazda RX4 Wag',1:6]
              mpg cyl disp  hp drat    wt
Mazda RX4 Wag  21   6  160 110  3.9 2.875
>
You can also modify the cells by indexing into the data frame
> df[5,5] <- 1000
> cars[5,5]
[1] 3.15
> df[5,]
                   mpg cyl disp  hp drat   wt  qsec vs am gear carb
Hornet Sportabout 18.7   8  360 175 1000 3.44 17.02  0  0    3    2
We can reference and also modify fields in the Data frame. Also notice that when we assign cars to df, it created a new deep copy. A change to df did not affect the source which is a constant.

Add Data

Often we need to add new rows and columns to the given data frame. R provides some functions to enable this functionality. R provides two methods - cbind and rbind to do this.

Add Row

Lets first check out the row bind functionality. To start with, pick up two parts of the mtcars dataset.
> df1 = mtcars[1:5, 1:5]
> df1
                   mpg cyl disp  hp drat
Mazda RX4         21.0   6  160 110 3.90
Mazda RX4 Wag     21.0   6  160 110 3.90
Datsun 710        22.8   4  108  93 3.85
Hornet 4 Drive    21.4   6  258 110 3.08
Hornet Sportabout 18.7   8  360 175 3.15
> 
> df2 = mtcars[6, 1:5]
> df2
         mpg cyl disp  hp drat
Valiant 18.1   6  225 105 2.76
Now, we can join these using rbind
> df <- rbind(df1, df2)
> df
                   mpg cyl disp  hp drat
Mazda RX4         21.0   6  160 110 3.90
Mazda RX4 Wag     21.0   6  160 110 3.90
Datsun 710        22.8   4  108  93 3.85
Hornet 4 Drive    21.4   6  258 110 3.08
Hornet Sportabout 18.7   8  360 175 3.15
Valiant           18.1   6  225 105 2.76
>

Add Column

Similarly, we can also join columns using the cbind command.
> df1 <- mtcars[1:5, 1:5]
> df2 <- mtcars[1:5, 6:7]
> 
> df1
                   mpg cyl disp  hp drat
Mazda RX4         21.0   6  160 110 3.90
Mazda RX4 Wag     21.0   6  160 110 3.90
Datsun 710        22.8   4  108  93 3.85
Hornet 4 Drive    21.4   6  258 110 3.08
Hornet Sportabout 18.7   8  360 175 3.15
> df2
                     wt  qsec
Mazda RX4         2.620 16.46
Mazda RX4 Wag     2.875 17.02
Datsun 710        2.320 18.61
Hornet 4 Drive    3.215 19.44
Hornet Sportabout 3.440 17.02
>
Now, we can merge these using the rbind method
> cbind(df1, df2)
                   mpg cyl disp  hp drat    wt  qsec
Mazda RX4         21.0   6  160 110 3.90 2.620 16.46
Mazda RX4 Wag     21.0   6  160 110 3.90 2.875 17.02
Datsun 710        22.8   4  108  93 3.85 2.320 18.61
Hornet 4 Drive    21.4   6  258 110 3.08 3.215 19.44
Hornet Sportabout 18.7   8  360 175 3.15 3.440 17.02
>
Ofcourse, for the cbind and rbind to work properly, the other dimension should match correctly. For example, while appending rows using rbind, the columns should match properly and vice-versa.

Column Names

Just as we can access the data in a data frame, we can also access the column names individually and also modify them. You can view the column names using the colnames function
> colnames(df)
[1] "mpg"  "cyl"  "disp" "hp"   "drat"
Since this is a vector, we can access also individual elements
> colnames(df)[2]
[1] "cyl"
>
We can also modify individual column names
> colnames(df)[1] <- "My Column"
>
Similarly, we can also modify all the column names at once
> colnames(df) <- c("Column 1", "Column 2", "Column 3", "Column 4", "Column 5" )
> colnames(df)
[1] "Column 1" "Column 2" "Column 3" "Column 4" "Column 5"
> df
                  Column 1 Column 2 Column 3 Column 4 Column 5
Mazda RX4             21.0        6      160      110     3.90
Mazda RX4 Wag         21.0        6      160      110     3.90
Datsun 710            22.8        4      108       93     3.85
Hornet 4 Drive        21.4        6      258      110     3.08
Hornet Sportabout     18.7        8      360      175     3.15
Valiant               18.1        6      225      105     2.76
>

Filter Data

You can also filter the data to get a subset of what is available in the data frame. For example, if we want to pull out only those cars that have 5 gears:
> mtcars[mtcars$gear == 5, ]
                mpg cyl  disp  hp drat    wt qsec vs am gear carb
Porsche 914-2  26.0   4 120.3  91 4.43 2.140 16.7  0  1    5    2
Lotus Europa   30.4   4  95.1 113 3.77 1.513 16.9  1  1    5    2
Ford Pantera L 15.8   8 351.0 264 4.22 3.170 14.5  0  1    5    4
Ferrari Dino   19.7   6 145.0 175 3.62 2.770 15.5  0  1    5    6
Maserati Bora  15.0   8 301.0 335 3.54 3.570 14.6  0  1    5    8
>
We can also use logical operators on the condition out there. For example, if we want an additional criteria that the car should also have 4 cylinders, we can do this:
> mtcars[mtcars$gear == 5 & mtcars$cyl > 4, ]
                mpg cyl disp  hp drat   wt qsec vs am gear carb
Ford Pantera L 15.8   8  351 264 4.22 3.17 14.5  0  1    5    4
Ferrari Dino   19.7   6  145 175 3.62 2.77 15.5  0  1    5    6
Maserati Bora  15.0   8  301 335 3.54 3.57 14.6  0  1    5    8
>
We can also perform statistical operations on this data:
> mean(mtcars[mtcars$hp > 100 & mtcars$wt > 2.5, ]$mpg)
[1] 16.86364
>

Conclusion

This is a very basic introduction to processing R data frames. There is a lot more to the language and you can check out the various References for more details.


R - Lists

Introduction Lists are the objects which contain elements of different types like numbers, strings, vectors, data frames and another list in...