For the gathering and spreading functions, the data needs to have an ID, so that R knows which information comes from the same sample. Gather the data we want to keep:
In this step, we’ll separate the values in millimeters and centimeters into separate columns with the separate() and into() functions. This allows you to create new columns.
The sep function allows to specify where or how you want the data to be separated. In this case, we will split the mm values (position 2 of the number) from the cm values.
If the original data is character, is interpreted as a regular expression. However, if the original data is numeric the sep argument is interpreted as positions to split at.
By specifying the convert=TRUE argument, R will run the type.convert() function internally and in this case convert the output to numeric, removing the decimal place.
This creates new NAs in the table, we will assign zero to those.
iris1.sep[is.na(iris1.sep)] <-0
The opposite of separate() function is the unite() function. In this step we will unite multiple columns into one, and reassign a numeric using the type.comvert() function
In this code we will create a new column, LS, from SepalCM and SepalMM, and add a decimal point.
Three key functions to organize or clean up data are select(), filter() and distinct(). These functions pick out objects based on their name (select), on their value (filter) or removes duplicates (distinct).
To remove duplicates distinct() can be used (however, there are not duplicates in the iris data frame)
iris2 <-distinct(iris1)
To remove duplicates based on a specified variable the following code can be used. .keep_all =TRUE indicates all other information needs to be kept, otherwise you would only get the 3 species names.
You an also order within groups if you first assign groups to the data set and include the .by_group=TRUE argument in the arrange() function. If you do not “turn on” the .by_group argument than the groups are ignored in the sorting.
With separate(), any of the following functions can be applied:
For example, iris3 can be updated to include a standard error (sd/sqrt(n)) of all 3 values.
* The SE for petal ratio is coded as follows: Petal.RatioSE=sd(Petal.Ratio, na.rm=T)/sqrt(n())
There are 5 main functions in dplyr to merge tables. These functions assume you have two tables, (x) and (y) , note that (x) is positioned left of (y).
Joining these two tables adds columns from the right dataset (y) to the left dataset (x) based on matching key columns:
Join Type
Function
Result
Inner Join
inner_join(x, y)
Keeps only rows that match in both x and y.
Left Join
Most common.
left_join(x, y)
Keeps all rows from x, matching rows from y (fills unmatched with NA).
Right Join
right_join(x, y)
Keeps all rows from y, matching rows from x (fills unmatched with NA).
Full Join
full_join(x, y)
Keeps all rows from both x and y, filling NA wherever there isn’t a match.
Let’s say another student in the lab gave you height data on the irises in data frame iris, however, plants with ID 5 to 10 died, so they measured 10 additional irises instead.
The new data can be found in the working directory, named irisbatch2.csv
SL SW PL PW Species ID plant.height
155 NA NA NA NA virginica 155 48.7
156 NA NA NA NA virginica 156 57.7
157 NA NA NA NA virginica 157 54.3
158 NA NA NA NA virginica 158 57.5
159 NA NA NA NA virginica 159 66.2
160 NA NA NA NA virginica 160 47.9
If we want to only keep the plant IDs that have values for all variables, we can choose inner join, and add %>% drop.na() . The resulting table has 144 rows.
R, %>% is called the pipe operator. It takes the output from the left side and passes it as the first argument into the function on the right side, it freely translates to ‘and then’.
irisinner <-inner_join(iris1, irisbatch2, by ="ID") %>%drop_na()head(irisinner)
Tables can also be stacked on top of each other, if the columns are matching.
Row-bind, rbind(, is the easiest to use. To add iris1 to iris1.order use:
combined <-rbind(iris1, iris1.order)
This results in a table of 300 rows and 6 variables.
However, rbind will give an error when there are non-matching column names in the new file. The function rbind.fill() can get around this, it is available in the plyr package
You have loaded plyr after dplyr - this is likely to cause problems.
If you need functions from both plyr and dplyr, please load plyr first, then dplyr:
library(plyr); library(dplyr)