1.1| File organization
1.2| Cottonwood
cottonwood <- read.csv("Data/cottonwood.csv")
1.3| Tree_height
Tree_height <- 15
Tree_circ <- pi*Tree_height
Tree_circ
## [1] 47.12389
2.1| Reading Questions
1| The authors recommend that “…spreadsheets are best suited to data entry and storage, and that analysis and visualization should happen separately.” Why do they make this recommendation? Do you agree?
The authors recommend keeping analysis and visualization separate from spreadsheets because working directly in the raw data file increases the risk of accidentally changing, contaminating, or deleting the original data, and I agree because keeping raw data untouched makes the analysis safer and more reproducible.
2| What is wrong with the way data are entered in this line? Genus = “Salvia”, Genus = “salvia”, Genus = “Salvai”
The three character entries above are not the same, the computer is case sensitive and spelling sensitive – so it would interpret each of the above as a separate species instead of the same species.
3| How can the line above be fixed?
Genus = “Salvia”, Genus = “Salvia”, Genus = “Salvia”, here each of the genus names are exactly the same, and interpreted by the computer as the same instead of as 3 different categories.
4| How are variable names arranged?
Variable names should be the first row, across the columns to describe what each column is. Each row would be an individual observation beneath the column variable label.
5| What are errors that you have made (or seen) while working with data tables? How can they be fixed?
Errors I have seen while working with data tables include inconsistent spelling or capitalization, blank cells, and entering information in the wrong column, and these can be fixed by checking the data carefully, using consistent formats, filling missing values with a standard code such as NA, and using data validation when possible.
2.2| To Do R Script
#importing my data
dirt <- read.csv("Data/Dirt01.csv")
#grabbing some columns from my data and undoing them into vectors
litter_treatment <- dirt$Treatment
vegtype <- dirt$Vegtype
organicC <- dirt$OC23
dirt.df <- data.frame(litter_treatment, vegtype, organicC) #put them into dataframe
head(dirt.df) #inspect
## litter_treatment vegtype organicC
## 1 CTL SB 2.18
## 2 CTL SB 1.32
## 3 CTL SB 1.88
## 4 CTL SB 1.11
## 5 CTL SB 1.14
## 6 CTL SB 0.70
ggplot(dirt.df, aes(litter_treatment, organicC, fill=vegtype))+ #make a pretty plot
geom_boxplot()+
labs(x="Treatment", y="Organic Carbon %", fill="Veg. Type")+
scale_fill_manual(values=c("gray", "#8BA49A"))
## Warning: Removed 33 rows containing non-finite outside the scale range
## (`stat_boxplot()`).
rabbit <- read.csv("Data/rabbit.csv")
1| Find the mean length of the Right Ear.
mean(rabbit$Right_Ear)
## [1] 10.7
2| Find the median Right Ear length.
median(rabbit$Right_Ear)
## [1] 10.65
3| Find the variance of the Right Ear length
var(rabbit$Right_Ear)
## [1] 0.2169231
4| Find the standard deviation of the Right Ear length
sd(rabbit$Right_Ear)
## [1] 0.46575
5| Use the tidyverse package to find all of the above descriptive statistics for the shampoo group and the not shampoo group
rabbit %>%
group_by(Shampoo)%>%
summarize(
mean(Right_Ear),
median(Right_Ear),
var(Right_Ear),
sd(Right_Ear))
## # A tibble: 2 × 5
## Shampoo `mean(Right_Ear)` `median(Right_Ear)` `var(Right_Ear)` `sd(Right_Ear)`
## <chr> <dbl> <dbl> <dbl> <dbl>
## 1 No 10.5 10.3 0.303 0.551
## 2 Yes 10.9 11 0.0733 0.271
6| Create a histogram of Right Ear length
ggplot(rabbit, aes(Right_Ear, fill=Shampoo))+
geom_density(alpha = 0.5)+
scale_fill_manual(values=c("plum", "blue"))+
theme_bw()+
labs(x = "Right Ear Length",y ="Density",fill = "Shampoo")
1| Green-shelled female beetles: 7
2| Average male beetle length: 49.01 mm
3| Beetles by substrate: grass 12, rock 11, sage 7
4| Total parasites: 1790
5| Width range: 0.88 mm
6| Dead beetles captured before June 1, 2007: 7
7| Why use Excel vs. Access?
I found Excel easier to use because the data, formulas, and results were all visible in one place. It worked well for a small dataset and made basic calculations quick. The downside is that it would be easy to accidentally edit a value or formula.
Access was a little harder to set up, but the queries were useful for pulling out specific information without changing the original table. I would probably choose Excel for something small like this, but Access seems more useful when the dataset gets bigger or more complex.
The following tabs correspond to our in class lab handouts.
Exercise 1
plants <- c("Orange", "Plum", "Lemon", "Peach", "Apple")
height <- as.numeric(c(5, 10, 4, 3, 2))
presence <- c(T, F, T, T, F)
width <- as.numeric(c(0.2, 0.5, 0.6, 0.2, 0.1))
df <- data.frame(plants, height, width, presence)
write.csv(df, "homemade_df_class4.csv")
cats <- read.csv("homemade_df_class4.csv", row.names = 1)
half <- cats$height*0.5
cats <- cbind(cats, half)
Exercise 2
new <- c(32, 14, "100")
str(new)
## chr [1:3] "32" "14" "100"
coerced <- as.numeric(new)
str(coerced)
## num [1:3] 32 14 100
dec <- c(5.2, 3.4, 4.4, 5.5, 3.4)
str(dec)
## num [1:5] 5.2 3.4 4.4 5.5 3.4
coerced_2 <- as.integer(dec)
str(coerced_2)
## int [1:5] 5 3 4 5 3
num <- c(3, 9, 11, 15)
coerced_3 <- as.character(num)
str(coerced_3)
## chr [1:4] "3" "9" "11" "15"
#if you want to create categories with your data..
Exercise 3
logical <- ifelse(cats$half >= 2, "Pass", "Fail")
cats <- cbind(cats, logical)
cats$logical
## [1] "Pass" "Pass" "Pass" "Fail" "Fail"
new <- factor(cats$logical, levels=c("Pass", "Fail"))
str(new)
## Factor w/ 2 levels "Pass","Fail": 1 1 1 2 2
DPLYR | 9.9.26Notes
library(dplyr) #needed for piping
#subset
sub_dirt <- dirt %>%
filter(OC18 >=0.5)%>%
filter(Vegtype=="SB")
head(sub_dirt)
## IDFull Vegtype Treatment Block Plot Depth ID X0.5 Name14
## 1 SB-CTL-1 (0-5) SB CTL 1 3 cm005 SB-CTL-1 5-10 003a
## 2 SB-CTL-1 (5-10) SB CTL 1 3 cm0510 SB-CTL-1 0-5 003b
## 3 SB-CTL-2 (0-5) SB CTL 2 5 cm005 SB-CTL-2 5-10 005a
## 4 SB-CTL-2 (5-10) SB CTL 2 5 cm0510 SB-CTL-2 0-5 005b
## 5 SB-CTL-3 (0-5) SB CTL 3 8 cm005 SB-CTL-3 5-10 008a
## 6 SB-CTL-3 (5-10) SB CTL 3 8 cm0510 SB-CTL-3 0-5 008b
## N14 TC14 OC14 InorgC14 X MSID18 MSweight18 OC18 N18
## 1 0.0872 1.0676 1.0660 0.0016 NA SB-1 30.436 1.4602126 0.12037273
## 2 0.0837 0.8579 0.9121 -0.0542 NA SB-2 28.068 0.9768002 0.09262667
## 3 0.1261 1.4745 1.3712 0.1033 NA SB-3 29.030 2.2796111 0.18017337
## 4 0.1256 1.3641 1.3180 0.0461 NA SB-4 31.786 0.8033978 0.07954627
## 5 0.0981 0.9132 0.8307 0.0825 NA SB-5 29.925 3.2378431 0.23041298
## 6 0.0453 0.8396 0.7036 0.1360 NA SB-6 29.883 0.9082777 0.08271213
## CNratio18 Analysis18 X.1 MSID23 MSweight23 OC23 N23 Analysis23
## 1 12.13076 16-Jan-25 NA SB-1 29.276 2.18 0.16 16-Jul-24
## 2 10.54556 16-Jan-25 NA SB-2 32.317 1.32 0.10 16-Jul-24
## 3 12.65232 16-Jan-25 NA SB-3 28.125 1.88 0.16 16-Jul-24
## 4 10.09975 16-Jan-25 NA SB-4 30.667 1.11 0.09 16-Jul-24
## 5 14.05235 16-Jan-25 NA SB-5 29.604 1.14 0.09 16-Jul-24
## 6 10.98119 16-Jan-25 NA SB-6 29.247 0.70 0.05 16-Jul-24
Exercise 1
trees <- read.csv("Data/cottonwood.csv")
head(trees)
## Leaf Age Length Width Fungus
## 1 1 sap 3.8 2.7 6
## 2 2 sap 10.0 6.0 0
## 3 3 sap 13.1 7.0 0
## 4 4 sap 9.0 5.9 1
## 5 5 mature 8.9 5.9 5
## 6 6 sap 9.0 6.9 0
#longest and shortest leaf length
range(trees$Length)
## [1] 3.1 18.8
#mean and median for fungus data
mean(trees$Fungus)
## [1] 4.7375
median(trees$Fungus)
## [1] 2
hist(trees$Fungus)
#variance of width
var_width<-var(trees$Width)
var_width
## [1] 2.973383
#create missing data value
trees[25,4]<-NA
#practice what to do if you have NA in your data
sd(trees$Width, na.rm=T)
## [1] 1.734463
#make histograms!
hist(trees$Fungus)
hist(trees$Width) #pretty normal!
hist(trees$Length) #also pretty normal
hist(trees$Leaf) #uniform!
Extra Challenge
trees <- trees %>%
mutate(Area=Length*Width)
Exercise 1
library(ape)
data(carnivora) #sstep 1
head(carnivora) #step 2
## Order SuperFamily Family Genus Species FW SW FB SB
## 1 Carnivora Caniformia Canidae Canis Canis lupus 31.1 33.1 130.0 132.3
## 2 Carnivora Caniformia Canidae Canis Canis latrans 9.7 10.6 84.5 88.3
## 3 Carnivora Caniformia Canidae Canis Canis adustus 10.6 11.3 53.5 51.8
## 4 Carnivora Caniformia Canidae Canis Canis mesomelas 7.2 7.7 52.0 56.8
## 5 Carnivora Caniformia Canidae Lycaon Lycaon pictus 22.2 22.0 128.0 129.0
## 6 Carnivora Caniformia Canidae Cuon Cuon alpinus 13.8 15.8 95.0 95.0
## LS GL BW WA AI LY AM IB
## 1 5.5 63.0 425 35 NA 177 913 12
## 2 6.2 61.5 225 98 NA NA 365 12
## 3 4.3 63.3 NA NA NA 127
## 4 3.8 60.0 NA 61 270 NA 392
## 5 8.8 70.5 365 77 390 132 t 132 13
## 6 4.3 62.0 275 NA NA 186
##STEP 3##
plot(carnivora$FW, carnivora$FB, main="Plot 1", xlab="Female Body Weight", ylab="Female Brain Weight", col="plum")
##STEP 4##
plot(carnivora$FW, carnivora$FB, main="Plot 1", xlab="Female Body Weight", ylab="Female Brain Weight", type="l")
##WHY USE POINT VS LINE?##
#line is better at showing averages and points can show you exacts...IDK, I definitely wouldnt use the plot in step 4 for these data...
##STEP 5##
boxplot(carnivora$Family, carnivora$FW)
##STEP 6##
hist(carnivora$BW, breaks = 5, col = "blue")
#The number of bins in the histograms changes to 5 from 13 when we change that value in the code.
Exercise 2
library(ggplot2)
##STEP 7-8##
ggplot(carnivora, aes(GL, BW))+
geom_point()+
labs(title="Body weight")
##STEP 9##
ggplot(carnivora, aes(GL, LS))+
geom_point()+
labs(title="Litter size")
##STEP 10##
ggplot(carnivora, aes(GL, LS))+
geom_line()+
labs(title="Litter size")
##STEP 11##
ggplot(carnivora, aes(GL, LS, color=Family))+
geom_point()+
labs(title="Litter size")
##STEP 12##
ggplot(carnivora, aes(GL, LS))+
geom_point(alpha=0.5)+
labs(title="Litter size", x="GL (log10)")+
geom_smooth()+
scale_x_log10()#get rid of big data so we can zoom in
Exercise 1
library(tidyverse)
data(iris)
iris %>%
glimpse()
## Rows: 150
## Columns: 5
## $ Sepal.Length <dbl> 5.1, 4.9, 4.7, 4.6, 5.0, 5.4, 4.6, 5.0, 4.4, 4.9, 5.4, 4.…
## $ Sepal.Width <dbl> 3.5, 3.0, 3.2, 3.1, 3.6, 3.9, 3.4, 3.4, 2.9, 3.1, 3.7, 3.…
## $ Petal.Length <dbl> 1.4, 1.4, 1.3, 1.5, 1.4, 1.7, 1.4, 1.5, 1.4, 1.5, 1.5, 1.…
## $ Petal.Width <dbl> 0.2, 0.2, 0.2, 0.2, 0.2, 0.4, 0.3, 0.2, 0.2, 0.1, 0.2, 0.…
## $ Species <fct> setosa, setosa, setosa, setosa, setosa, setosa, setosa, s…
#the pipe is quickly telling us the data types, column names, and first few values for each column in the iris dataset
iris %>%
filter(Species =="setosa")%>%
count(Sepal.Length>5.1)
## Sepal.Length > 5.1 n
## 1 FALSE 36
## 2 TRUE 14
petal.data <- iris %>%
select(Species, Petal.Length, Petal.Width)%>%
rename(Length=Petal.Length, Width=Petal.Width)%>%
arrange(Length)
head(petal.data)
## Species Length Width
## 1 setosa 1.0 0.2
## 2 setosa 1.1 0.1
## 3 setosa 1.2 0.2
## 4 setosa 1.2 0.2
## 5 setosa 1.3 0.2
## 6 setosa 1.3 0.4
iris <- iris%>%
mutate(
Sepal.Area=Sepal.Length*Sepal.Width,
Petal.Area=Petal.Length*Petal.Width)
ggplot(iris, aes(Petal.Area, Sepal.Area, color=Species))+
geom_point()+
labs(title="Step 6", x="Petal Area", y="Sepal Area")