Help understand the error in a function I defined in R
The aes
function in ggplot2 uses names like library()
does, i.e. it takes the name of the argument as the argument. If this is an object, it does not evaluate it but takes the name instead. Here it takes varx
as the argument and not what varx
evaluates too.
It works if you use aes_string()
instead and use characters as arguments in the fboxplot()
call:
fboxplot <- function(mydataframe, varx, vary)
{
p <- ggplot(data=mydataframe, aes_string(x=varx, y=vary))
p + geom_boxplot()
}
col1 = factor(rep(1:3, 3))
col2 = rnorm(9)
col3 = c(rep(10,5), rep(20,4))
df = data.frame(col1 = col1, col2 = col2, col3 = col3)
fboxplot(df, "col1", "col2")
Error: could not find function ... in R
There are a few things you should check :
- Did you write the name of your function correctly? Names are case sensitive.
- Did you install the package that contains the function?
install.packages("thePackage")
(this only needs to be done once) - Did you attach that package to the workspace ?
require(thePackage)
(and check its return value) orlibrary(thePackage)
(this should be done every time you start a new R session) - Are you using an older R version where this function didn't exist yet?
- Are you using a different version of the specific package? This could be in either direction: functions are added and removed over time, and it's possible the code you're referencing is expecting a newer or older version of the package than what you have installed.
If you're not sure in which package that function is situated, you can do a few things.
- If you're sure you installed and attached/loaded the right package, type
help.search("some.function")
or??some.function
to get an information box that can tell you in which package it is contained. find
andgetAnywhere
can also be used to locate functions.- If you have no clue about the package, you can use
findFn
in thesos
package as explained in this answer. RSiteSearch("some.function")
or searching with rdocumentation or rseek are alternative ways to find the function.
Sometimes you need to use an older version of R, but run code created for a newer version. Newly added functions (eg hasName in R 3.4.0) won't be found then. If you use an older R version and want to use a newer function, you can use the package backports to make such functions available. You also find a list of functions that need to be backported on the git repo of backports. Keep in mind that R versions older than R3.0.0 are incompatible with packages built for R3.0.0 and later versions.
R Function error
This is how your function should look like. But you didn't define noofNA
, so you will still get an error.
details <- function(x)
{
na.len <- sum(is.na(x))
m <- mean(x, na.rm=TRUE)
s <- sd(x, na.rm=TRUE)
mn <- min(x, na.rm=TRUE)
q1 <- quantile(x, 0.25, na.rm=TRUE)
q3 <- quantile(x, 0.75, na.rm=TRUE)
mx <- max(x, na.rm=TRUE)
UC1 <- m+3*s
LC1 <- m-3*s
UC2 <- quantile(x, 0.99, na.rm=TRUE)
LC2 <- quantile(x, 0.01, na.rm=TRUE)
iqr <- IQR(x, na.rm=TRUE)
UC3 <- q3+1.5*iqr
LC3 <- q1-1.5*iqr
ot <- mx>UC1 | mn<LC1 | mx>UC2 | mn<LC2 | mx>UC3 | mn<LC3
x[x>mx]<-mx
x[x<mn]<-mn
out_exist <- ifelse(noofNA > 0, "outlier_exists", "")
return(list(noofNA=na.len, mean=m, std=s, min=mn, q1=q1, q3=q3, max=mx, outlier=ot, out_exists= out_exist))
}
set.seed(123)
df1 <- data.frame(x = rnorm(100), y = rnorm(100))
sapply(df1, details)
Trying to understand R error: Error in FUN(X[[i]], ...) : only defined on a data frame with all numeric variables
Perhaps you should be counting the number of rows in good data, instead of trying to sum an entire data frame.
if (nrow(complete_data) >= threshold) {
b <- cor(sulfate,nitrate)
vect1 <- rbind(b)
}
Error: could not find function %%
You need to load a package (like magrittr
or dplyr
) that defines the function first, then it should work.
install.packages("magrittr") # package installations are only needed the first time you use it
install.packages("dplyr") # alternative installation of the %>%
library(magrittr) # needs to be run every time you start R and want to use %>%
library(dplyr) # alternatively, this also loads %>%
The pipe operator %>%
was introduced to "decrease development time and to improve readability and maintainability of code."
But everybody has to decide for himself if it really fits his workflow and makes things easier.
For more information on magrittr
, click here.
Not using the pipe %>%
, this code would return the same as your code:
words <- colnames(as.matrix(dtm))
words <- words[nchar(words) < 20]
words
EDIT:
(I am extending my answer due to a very useful comment that was made by @Molx)
Despite being from
magrittr
, the pipe operator is more commonly used
with the packagedplyr
(which requires and loadsmagrittr
), so
whenever you see someone using%>%
make sure you shouldn't loaddplyr
instead.
I do not understand error object not found inside the function
The problem is that splom
evaluates its groups
argument in a nonstandard way.A quick fix is to rewrite your function so that it constructs the call with the appropriate syntax:
f <- function(data, id)
eval(substitute(splom(data, groups=.id), list(.id=id)))
# test it
ir <- iris[-5]
sp <- iris[, 5]
f(ir, sp)
Object not found error within a user defined function, eval() function?
Try this. You need to keep in mind that R doesn't know what's my.time
and my.event
. You have to parse them with quotes and then unqoute them in order to parse it into Surv
myfun<-function(dflist,time,event){
for (i in 1:length(dflist)){
time <- noquote(time)
event <- noquote(event)
out<-cch(Surv(dflist[[i]][, time], dflist[[i]][, event])~as.factor(my.det), data=dflist[[i]],
subcoh=~sub, id=~my.id, cohort.size=500)
print(out)}
}
myfun(my.list,"my.time","my.event")
Related Topics
Scraping JavaScript Generated Data
Replace Na with Grouped Means in R
Importing Multiple .CSV Files with Variable Column Types into R
How to Unlock Environment in R
Convert from N X M Matrix to Long Matrix in R
R Shiny - Ui.R Seems to Not Recognize a Dataframe Read by Server.R
Use Hooks to Format Table in Output
R: Finding the Intersect of Two Lines
Select a Sequence of Columns: ':' Works But Not 'Seq'
As.Date Produces Unexpected Result in a Sequence of Week-Based Dates
How to Configure R-3.0.1 with --Enable-R-Shlib
Adding an Image to a Datatable in R
Manual Simulation of Markov Chain in R
Place 1 Heatmap on Another with Transparency in R
How to Prevent Blogdown from Rerendering All Posts
Why Are Probabilities and Response in Ksvm in R Not Consistent