How to Create and Manipulate Lists and Data frames in R
How to create and manipulate Lists
Step 1. We recommend you to work in the same working sub-directory that you created previously, using one of the following optionsBefore launching R$ cd exerciseR
$ pwd
/Users/imac/Desktop/exerciseR
$ R
> setwd("/Users/imac/Desktop/exerciseR")
> getwd()
[1] "/Users/imac/Desktop/exerciseR"
> L<-list(dairy="milk",type="almond",form="liquid",contain.liter=c(0.5,1,2))
> L<-list(dairy="milk",type="almond",form="liquid",contain.liter=c(0.5,1,2))
>> L
$dairy
[1] "milk"$type
[1] "almond"$form
[1] "liquid"$contain.liter
[1] 0.5 1.0 2.0
> length(L)
[1] 4
> L[[1]]
[1] "milk"
> L[[2]]
[1] "almond"
> L[[3]]
[1] "liquid"
> L[[4]]
[1] 0.5 1.0 2.0
>> L[[4]][[1]]
[1] 0.5
> L$dairy
[1] "milk"
> L[[1]]
[1] "milk"
> List.A <- list(dairy="milk", type="almond")
> List.B <- list(dairy="yogurt", type="frozen")
> List.AB <- c(List.A, List.B)
> List.AB
$dairy
[1] "milk"$type
[1] "almond"$dairy
[1] "yogurt"$type
[1] "frozen"
How to create and manipulate Data frames
Step 1. Now that we know how Variables, Vectors, and Lists can be created and manipulated, let’s use this knowledge to create sequentially a data frame called df.> Name <- c("Lilly", "James", "Harry")
> Age <- c(30, 31, 11)
> Height <- c(168, 179, 139)
> Weight <- c(57, 69, 32)
> df <- data.frame (row.names = Name, Age, Height, Weight)
> Name <- c("Lilly", "James", "Harry")
> Age <- c(30, 31, 11)
> Height <- c(168, 179, 139)
> Weight <- c(57, 69, 32)
> df <- data.frame (row.names = Name, Age, Height, Weight)
> dfAge Height Weight
Lilly 30 168 57
James 31 179 69
Harry 11 139 32
> Name <- c("Lilly", "James", "Harry")
> Sex <- c("F", "M", "M")
> df.add <- data.frame(row.names = Name, Sex)
> df.addSex
Lilly F
James M
Harry M
> df.all <- cbind(df, df.add)
> df.allAge Height Weight Sex
Lilly 30 168 57 F
James 31 179 69 M
Harry 11 139 32 M
> Name <- c("Lilly", "James", "Harry")
> Sex <- as.factor(c("F", "M", "M"))
> df.add.fact <- data.frame(row.names = Name, Sex)
> df.all.fact <- cbind(df, df.add.fact)
> df.all.factAge Height Weight Sex
Lilly 30 168 57 F
James 31 179 69 M
Harry 11 139 32 M
> class(Sex)
[1] "factor"
> levels(Sex)
[1] "F" "M"
> levels(df.all$Sex) <- c("M", "F")
> df.allAge Height Weight Sex
Lilly 30 168 57 F
James 31 179 69 M
Harry 11 139 32 M
>> levels(df.all.fact$Sex) <- c("M", "F")
> df.all.factAge Height Weight Sex
Lilly 30 168 57 M
James 31 179 69 F
Harry 11 139 32 F
> levels(Sex)
[1] "F" "M"
> nlevels(Sex)
[1] 2
- How many rows and columns are in the data frame df.all.fact ? you can use dim() to set or get the dimension of the data frame (rows, columns), or more specifically nrow() for the number of columns and ncol() for the number of columns.
> dim(df.all.fact)
[1] 3 4
> nrow(df.all.fact)
[1] 3
> ncol(df.all.fact)
[1] 4
> sapply(df.all.fact, class)Age Height Weight Sex"numeric" "numeric" "numeric" "factor">> str(df.all.fact)
'data.frame': 3 obs. of 4 variables:$ Age : num 30 31 11$ Height: num 168 179 139$ Weight: num 57 69 32$ Sex : Factor w/ 2 levels "M","F": 1 2 2
> df.all.fact[1]Age
Lilly 30
James 31
Harry 11
> df.all.fact[,1]
[1] 30 31 11
>> df.all.fact[1,]Age Height Weight Sex
Lilly 30 168 57 M
> df.all.fact[1,1]
[1] 30
>> df.all.fact[1:3,3]
[1] 57 69 32
> df.all.factAge Height Weight Sex
Lilly 30 168 57 M
James 31 179 69 F
Harry 11 139 32 F
> df.all.fact[order(df.all.fact$Height),]Age Height Weight Sex
Harry 11 139 32 F
Lilly 30 168 57 M
James 31 179 69 F
> unique(df.all.fact$Age)
[1] 30 31 11
>> sort(unique(df.all.fact$Age))
[1] 11 30 31

Bioinformatics for Biologists: An Introduction to Linux, Bash Scripting, and R

Our purpose is to transform access to education.
We offer a diverse selection of courses from leading universities and cultural institutions from around the world. These are delivered one step at a time, and are accessible on mobile, tablet and desktop, so you can fit learning around your life.
We believe learning should be an enjoyable, social experience, so our courses offer the opportunity to discuss what you’re learning with others as you go, helping you make fresh discoveries and form new ideas.
You can unlock new opportunities with unlimited access to hundreds of online short courses for a year by subscribing to our Unlimited package. Build your knowledge with top universities and organisations.
Learn more about how FutureLearn is transforming access to education