-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathRegex.Rmd
More file actions
40 lines (28 loc) · 1.28 KB
/
Copy pathRegex.Rmd
File metadata and controls
40 lines (28 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
---
title: "Regex"
author: "Caleb Pollock"
date: '2023-02-08'
output: html_document
---
https://github.com/caleb-pollock/BIOL432_Assignment1
1. Create the Regex.rmd and add code for each of the following steps
-Import your measurements.csv file from the Week 1 Assignment into an object called MData
-Use a dplyr command AND regular expression(s) to add a new column called Sp that shortens the genus name in the Organism column to only the first letter and period. For example: Homo sapiens in the original Organism column becomes H. sapiens in the Sp column. HINT: You can think of the regular expression commands like sub, grep, grepl and gsub as 'functions' that you can use with dplyr commands just like mean(), sd(), and desc() in the Data Science tutorial.
-Use a dplyr command to create a new dataset BUT with Sp column instead of the Organism column, moved to the beginning in place of the original Organism column.
-Save the dataset as a new file called MeasShort.csv
```{r}
library(dplyr)
```
```{r}
MData <- read.csv("measurements.csv")
MData <- MData %>%
mutate(Sp = sub("^([A-Z]).* ([a-z]+)", "\\1. \\2", Organism))
head(MData)
```
```{r}
MeasShort <- MData[,-1]
MeasShort <- MeasShort %>%
select(Sp, everything())
head(MeasShort)
write.csv(MeasShort)
```