Debugging Excercises
In the following examples, evaluate each problem using the criteria of a minimal reproducible example from this StackOverflow post.
Specifically, address the following things:
Are the question and problem description complete?
Is the issue reproducible? Does it have a description of the problem, with code that reproduces the problem? What could improve it?
Is the example minimal? Does it include extra code or information that is not necessary?
Does the question describe any attempted solutions?
Then, using what you know about R and Python, try to debug the problem using the strategies in the debugging chapter. Once you are thoroughly stumped, or you believe you have solved the problem, compare your solutions to those given on StackOverflow using the link.
Why Doesn’t this R function work?
I created the following function to append new strings on a vector of strings called “meals”. However, when I use this function to append an string input into my “meals” vector, it does not work.
add <- function(str) {
meals <- append(meals, as.character(str))
}
Link to original SO post.
Function not working R
I’ve never programmed before and am trying to learn. I’m following that “coursera” course that I’ve seen other people post about — a course offered by Johns Hopkins on R programming.
Anyway, this was supposed to be my first function. Yet, it doesn’t work! But when I type out all the steps individually, it runs just fine… Can anyone tell me why?
pollutantmean <- function(directory, pollutant, id = 1:332){
x<- list.files("/Users/mike******/Desktop/directory", full.names=TRUE)
y<- lapply(x, read.csv)
z<- do.call(rbind.data.frame, y[id])
mean(z$pollutant, na.rm=TRUE)
}
pollutantmean(specdata,nitrate,1:10)
[1] NA
Warning message:
In mean.default(z$pollutant, na.rm = TRUE) :
argument is not numeric or logical: returning NA
####
x<- list.files("/Users/mike******/Desktop/specdata",full.names=TRUE)
y<- lapply(x,read.csv)
z<- do.call(rbind.data.frame,y[1:10])
mean(z$nitrate,na.rm=TRUE)
[1] 0.7976266
Inexplicable error when trying to export my R notebook
Getting this error from R Markdown when trying to export my .RMD
"Error in filter(Gastropods, Species == "Cellana") : object 'Species' not found Calls: <Anonymous> ... withCallingHandlers -> withVisible -> eval -> eval -> filter"
However, all my plots are coming out successfully. I can clearly see in the data that the species column is there and that Cellana is a species. No spelling errors or anything.
My first 20 or so lines of code are below
###
---
title: " Lab Report 2 - z5016113"
output: html_notebook
i---
#1. Gastropod abundance vs. height on the shore
```{r}
Gastropods <- read.csv(file = "MaroubraZones.csv", header = TRUE)
library(ggplot2, dplyr)
```
```{r}
Gastropods$Zone <- factor(Gastropods$Zone, levels = c("Low", "Mid", "High"))
```
```{r}
Cellana <- filter(Gastropods, Species == "Cellana") ------> This line is causing the error
```
```{r}
ggplot(Cellana, aes(Zone, Abundance)) + geom_boxplot()
```
###
You can find this sample file here
Python if elif else statement
I’m trying to create a program with python that calculate the cost for shipping.
However, I can’t run the program to where it works properly.
What ever my total is the same amount comes out as $6 for US and $8 for Canada. I can’t seem to get pass that.
Example:
total = raw_input('What is the total amount for your online shopping?')
country = raw_input('Shipping within the US or Canada?')
if country == "US":
if total <= "50":
print "Shipping Costs $6.00"
elif total <= "100":
print "Shipping Costs $9.00"
elif total <= "150":
print "Shipping Costs $12.00"
else:
print "FREE"
if country == "Canada":
if total <= "50":
print "Shipping Costs $8.00"
elif total <= "100":
print "Shipping Costs $12.00"
elif total <= "150":
print "Shipping Costs $15.00"
else:
print "FREE"