gge-ucd / Discussion

Class discussion for R-DAVIS course
0 stars 4 forks source link

error unused argument y #53

Closed caparisek closed 3 years ago

caparisek commented 5 years ago

I'm trying to calculate some simple metrics and I think I found a simple enough package that could do it for me - the trouble is there is next to no help-pages on troubleshooting errors so I can't figure out what's going wrong. I keep getting Error in laymanMetrics(., x, y) : unused argument (y). Does anyone see right off the bat why this is happening? I'd really appreciate some advice!

The package is meant to use x,y (d13C,d15N) data to calculate some metrics.


# laymen metric R package: https://rdrr.io/cran/SIBER/man/laymanMetrics.html 
# https://cran.r-project.org/web/packages/SIBER/SIBER.pdf
# https://github.com/AndrewLJackson/SIBER/blob/master/R/laymanmetrics.R 

install.packages("SIBER")  #Stable Isotope Baysian Ellipses in R (SIBER)
install.packages("tidyverse")
library(SIBER)
library(tidyverse)
library(stats)

Lakes2014 <- read.csv("data/R2014IsotopeData.csv")

Lakes2014 %>% 
  x<-Lakes2014$d13C_maybe %>% 
  y<-Lakes2014$d15N_maybe %>% 
  laymanMetrics(x,y) ```

@ryanpeek @MCMaurer @MarthaWohlfeil 
ryanpeek commented 5 years ago

I'll take a look...one quick note, I just happened to see this because I was looking at a few other things in the discussion repo...if you notice the @ryanpeek @MCMaurer @MarthaWohlfeil bit from your post didn't actually flag us, because it's still part of your code chunk. So make sure the 3 backticks get their own line, and that the @ tags go outside of that code chunk otherwise this happens:

# some more code```
@caparisek

As to your question, I suspect it has to do with the pipes that you are using/assigning, typically this sort of assignment won't work when using pipes, because you are passing what's on the left to the right...so after you pass the dataframe and assign something, it's not able to "reassign" that thing and pass it all to a function. See my comments below:

# original code with comments:
Lakes2014 %>%  # this is passing your data on
  x<-Lakes2014$d13C_maybe %>% # this is trying to assign a column from your data to x, but after a pipe above it will break
  y<-Lakes2014$d15N_maybe %>%  # same as above
  laymanMetrics(x,y) 

So try something more like this, no need for pipes:

# assign your data to x and y
x<-Lakes2014$d13C_maybe 
y<-Lakes2014$d15N_maybe

# pass x and y to function
laymanMetrics(x,y)
caparisek commented 5 years ago

Oh, I didn't notice the last backticks didn't close the code chunk! Thanks for pointing that out

Ryan, you fixed it!! Wow, I hoped it was something so straightforward. THE CODE WORKS. I'm so excited.

@ryanpeek, thank you!! 🎉🐟

caparisek commented 5 years ago

On this same thread - I have another question.

I need to do these metrics for 12 separate lakes. Currently, all lakes are in the same datasheet.

Is there a way we haven't learned yet, to add on to this "$" method of calling a column in a dataset that also selects for ONLY lakes called "Swamp", for example? Where I can iterate it (manually) do to this metric for all 12 lakes, separately.

x<-Lakes2014$d13C_maybe # WOW y<-Lakes2014$d15N_maybe laymanMetrics(x,y)

ryanpeek commented 5 years ago

Glad it works! As to the question, could do it two ways, one using base R with the [ ] to subset data, or using dplyr::filter().

Base R

x <- Lakes2014$d13C_maybe[Lakes2014$d13C_maybe %in% c("Lake1", "Lake2", etc)]
class(x) # numeric

Returns a vector

dplyr

x.data.frame <- Lakes2014 %>% select(d13C_maybe) %>% 
    filter(d13C_maybe %in% c("Lake1", "Lake2", ...))
class(x.data.frame) # a data frame with a single column

x.dplyr <- Lakes2014 %>% select(d13C_maybe) %>% 
    filter(d13C_maybe %in% c("Lake1", "Lake2", ...)) %>% 
    pull(d13C_maybe)
class(x.dplyr) # now a numeric vector as above

Without the pull() dplyr returns a data frame...so to end up with just a single vector, use pull

To iterate through these you could either use a for loop, or one of the map functions in purrr to go through and apply the function to each lake of interest. I'm not familiar with the package/function, but you may try a group by lake and then apply the function that way as well in dplyr...but would need to check how the function works first.

Hope this helps.