New to R Studio, given code to work with but need to change varibles

I have code below that works just fine with the .xlxs files given, just trying to figure out how to incorporate new .xlxs files so I can get some new bar graphs with standard error
I keep getting this: Error: path does not exist: ‘smails.xlsx’

#amphibian mass
library(readxl)
amphibian_mass <- read_excel("amphibian_mass.xlsx")
View(amphibian_mass) 
amphib_mass = amphibian_mass

#calculate the mean amphibian mass by tank
mean_by_tank = aggregate(Mass ~ Tank + Treatment_Code +Species, data=amphib_mass, mean)
mean_by_tank

#calculate the mean mass by treatment
mean_by_trt = aggregate(Mass ~ Species + Treatment_Code, data=mean_by_tank, mean)
mean_by_trt

#Standard Error function
st.err <- function(x) {
  sd(x)/sqrt(length(x))
}

#Calculate the standard error of the mean mass of amphibians by tank
SE <- aggregate(Mass ~ Species + Treatment_Code, data=mean_by_tank, st.err)
SE

#combine the data to put into one table
mean_by_trt$SE = SE$Mass
mean_by_trt

#plotting library
library(ggplot2)

#define the standard error limits for the creation of the error bars
ymax = mean_by_trt$Mass + mean_by_trt$SE
ymin = mean_by_trt$Mass - mean_by_trt$SE

#create plot (p) of the mean ambiphian mass by treatment with standard error bars
p = ggplot(mean_by_trt, aes(fill=Treatment_Code, y=Mass, x=Species))

p + geom_bar(position=position_dodge(), stat="identity", colour='black') +
  geom_errorbar(aes(ymin=ymin, ymax=ymax), width=.2,position=position_dodge(.9)) +
  xlab("Species") +
  ylab("Mass (g)") +
  scale_fill_discrete(name= "Treatment", labels = c("natives", "bullfrog", "mosquitofish", "bullfrog + mosquitofish"))

##this is a .R file, not a .RMD file. To create a markdown pdf from this file, you can 'Compile Report' using Contrl+Shift+k

This code was used to graph the mass on the y-axis and species on the x-axis. I have new data (snails.xlxs) that I want to incorporate into this code, do I just replace everything that says mass with Num-Snails? or just snails? 
I keep getting --  Error: `path` does not exist: ‘smails.xlsx’

here is my new .xlxs file:
Tank	Treatment_Code	Num_Snails	shell_size
1	                      D	               391	        4.7578
2                            B	               112	        7.3802
3	                      C	               455	        5.1552
4	                      A	               917	        3.5896
5	                      B	               432	        3.8824 
6	                      B	               538	        3.4698

the original mass .xlxs:
Tank	Treatment_Code	Species	Mass
1	                   D	                PSRE	0.33
2	                   B	                 PSRE	0.57
2	                   B	                 PSRE	0.42
2	                   B	                 PSRE	0.26
etc...




Description of issue -

System Information:

  • RStudio Edition: (Desktop or Server)
  • RStudio Version: 3.5.1
  • OS Version:
  • R Version:
  • sessionInfo():

Referred here from support.rstudio.com

Hi, and welcome!

To see if smails.xlsx or any other file exists, enter

dir()

which will list all of the files and directories in your current environment. The file you are looking for might be in a directory, say data, in which case it would need to be

snails <- read_excel("data/snails.xlsx")

Of course, it might be somewhere else entirely, in which case you'd need to give the full pathname.

Thank you for the reply! I did find the snails.xlsx! I am assuming my next step is rename parts to match up with snails data instead of mass data? I tried this and errors popped up:

I ran this code
#calculate the mean Num_Smail by tank
mean_by_tank = aggregate(Num_Snail ~ Tank + Treatment_Code +Species, data=snails, mean)
mean_by_tank
and got this
#calculate the mean Num_Smail by tank

mean_by_tank = aggregate(Num_Snail ~ Tank + Treatment_Code +Species, data=snails, mean)
Error in eval(predvars, data, env) : object 'Num_Snail' not found

Not sure what I am doing

If you look at your dataframe 'snail', there must be a variable Num_snail in it for the aggregate function call to work. Is there ?

1 Like

Yes, there is,
Tank Treatment_Code Num_Snails shell_size
1 D 391 4.7578
2 B 112 7.3802
3 C 455 5.1552
4 A 917 3.5896
5 B 432 3.8824
6 B 538 3.4698

The token Num_Snails is not equal to the token Num_snail

In R capitalisation is important (even after accounting for the extra letter s )
Hope it helps

1 Like

Thanks, it is those little things....

1 Like

Alas, all too true. One way to alleviate this type of problem is to keep column names simple. When I have hard-to-distinguish names, I like to set them aside in favor of clearer alternatives.

old_head <- colnames(my_df)

Then I invent some more convenient names

new_head <- c("kwh","cooling","heating")

and substitute them

colnames(my_df) <- new_head

go about my work, then put names back as they were

colnames(my_df) <- old_head

good idea, thanks for all the advice!

1 Like

Getting athother error I can't seem to shake.

#create plot (p) of the mean Number Phytoplankton by treatment with standard error bars

p = ggplot(mean_by_tank, aes(fill=Treatment_Code, y=Phytoplankton_Fluorescence, x=Tank))
p + geom_bar(position=position_dodge(), stat="identity", colour='black') +

  • geom_errorbar(aes(ymin=ymin, ymax=ymax), width=.2,position=position_dodge(.9)) +
  • xlab("Species") +
  • ylab("Relative Phytoplankton Fluorescence") +
  • scale_fill_discrete(name= "Treatment", labels = c("natives", "bullfrog", "mosquitofish", "bullfrog + mosquitofish"))
    Error: Aesthetics must be either length 1 or the same as the data (20): ymin, ymax

Data:

here is the entire code for the last error:

#phytoplankton
library(readxl)
snails <- read_excel("Applied_Ecology_Activity _1/phytoplankton.xlsx")

View(phytoplankton)

#calculate the mean phytoplankton by tank
mean_by_tank = aggregate(Phytoplankton_Fluorescence ~ Tank + Treatment_Code, data=phytoplankton, mean)
mean_by_tank

#calculate the mean phytoplankton by treatment
mean_by_trt = aggregate(Phytoplankton_Fluorescence ~ Treatment_Code, data=mean_by_tank, mean)
mean_by_trt

#Standard Error function
st.err <- function(x) {
sd(x)/sqrt(length(x))
}

#Calculate the standard error of the mean phytoplankton by tank
SE <- aggregate(Phytoplankton_Fluorescence ~ Treatment_Code, data=mean_by_tank, st.err)
SE

#combine the data to put into one table
mean_by_trt$SE = SE$Phytoplankton_Fluorescence
mean_by_trt

#plotting library
library(ggplot2)

#define the standard error limits for the creation of the error bars
ymax = mean_by_trt$Phytoplankton_Fluorescence + mean_by_trt$SE
ymin = mean_by_trt$Phytoplankton_Fluorescence - mean_by_trt$SE

#create plot (p) of the mean Number Phytoplankton by treatment with standard error bars
p = ggplot(mean_by_tank, aes(fill=Treatment_Code, y=Phytoplankton_Fluorescence, x=Tank))

p + geom_bar(position=position_dodge(), stat="identity", colour='black') +
geom_errorbar(aes(ymin=ymin, ymax=ymax), width=.2,position=position_dodge(.9)) +
xlab("Species") +
ylab("Relative Phytoplankton Fluorescence") +
scale_fill_discrete(name= "Treatment", labels = c("natives", "bullfrog", "mosquitofish", "bullfrog + mosquitofish"))

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.