R/DataMixer.R

## BIOL 432: Assignment 2
## Claire Smith 10187239

library(dplyr)

## 1. Read in a file named InData.csv.
dat <- read.csv("InData.csv")

## 2. Remove rows with ‘Total’ biomass < 60.
## 3. Reorder the columns so that they are in the order:
# ‘Total’, ‘Taxon’, ‘Senario’, ‘Nutrients’, and remove
# the other columns.
## 4. Make a new column TotalG, which converts
# the ‘Total’ column from mg to grams AND replace
# Total with TotalG.

dat2 <- dat %>%
  filter(Total >= 60) %>%
  select(Total, Taxon, Scenario, Nutrients) %>%
  mutate(TotalG = Total / 1000) %>%
  select(TotalG, Taxon, Scenario, Nutrients)

## 5. If there is a column called ‘Nutrients’, replace each
# string with its first letter – for example ‘high’ becomes
# ‘h’, ‘moderate’ becomes ‘m’, etc. (You don’t know what all
# the options might be).

count <- nrow(dat2)

NutrientVector <- c()
if ("Nutrients" %in% names(dat2)) {
  for (i in 1:count) {
    NutrientVector[i] <- (gsub("(\\w)\\w+", "\\1", dat2$Nutrients[i]))
  }
}
dat2$Nutrients <- factor(NutrientVector)

## 6. Replace all periods . with commas , in the ‘TotalG’ column.

TotalGVector <- c()
for (i in 1:count){
  TotalGVector[i] <- (gsub("\\.", ",", dat2$TotalG[i]))
}
dat2$TotalG <- TotalGVector
claireasmith/Assignment2Repository documentation built on May 7, 2019, 9:20 p.m.