could not find function "is.square.matrix" [duplicate] - r

This question already has answers here:
Error: could not find function ... in R
(10 answers)
Closed 1 year ago.
I am not able to use is.square.matrix() function for my matrix in R.
library(tidyverse)
install.packages(c("readr", "dplyr", "haven"))
install.packages(c("psych"))
install.packages("matlib")
matr <- cov(na.omit(airquality))
is.square.matrix(matr)
I am getting error for this function could not find is.square.matrix. Can someone help mw in figuring out what I am missing here?

is.square.matrix seems to be from matrixcalc package
# // if not installed, then install the package and load it
#install.packages('matrixcalc')
library(matrixcalc)
is.square.matrix(matr)
[1] TRUE
If we don't know from which package it is loaded, then use ?? to do the check on R console and then it opens a webpage with the help pages of similar/exact functions
??is.square.matrix
gives

Related

Is it possible to get the environment that a value originated from? [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do you you determine the namespace of a function?
I don't know how to do this... How do you know the package name for a certain function in R? I would like to have a function that given the name of a function, returns the name of the package that owns it. Any suggestion?
There may be better solutions, but find("functionname") seems to work reasonably well? However, it only works for loaded packages.
> find("strwidth")
[1] "package:graphics"
> find("qplot")
character(0)
> library(ggplot2)
> find("qplot")
[1] "package:ggplot2"
>
(If you need the raw name of the package you can use gsub("^package:","",results))
(The answers to the previous question linked by Andrie include this answer; they don't give the bit about gsub, and they all seem to share the issue of not finding non-loaded packages.)
Here's a quick hack to find functions even in non-loaded packages:
findAllFun <- function(f) {
h <- help.search(paste0("^",f,"$"),agrep=FALSE)
h$matches[,"Package"]
}
findAllFun("qplot")
## "ggplot2"
findAllFun("lambertW")
## "emdbook" "VGAM"
> findAllFun("xYplot")
## "Hmisc" "lattice"
If you need to find functions in non-installed packages (i.e. searching CRAN), then findFn from the sos package will be your friend.

load packages in a loop via `library()` [duplicate]

This question already has answers here:
Load multiple packages at once
(10 answers)
Closed 3 years ago.
Instead of loading 30 packages each with the library function is it possible to do this in a loop?
pckgs = c("readr", "dplyr")
sapply(pckgs, library)
Background:
Before loading, i test if the packages are installed. For doing so i already have the package names in the form of c("readr", ..., "dplyr") and was wondering if i can also load the package in a loop instead of writing 30 times library().
What i tried:
I simplified to one package:
sapply("readr", library)
sapply("readr", function(lib) library(lib))
sapply("readr", function(lib) library(get(lib)))
Spoiler:
I wanted to post this question and then decided to check for a "force character" in the parameter and got lucky. (It is a bit weird asking a question and answering it yourself, but when i read this i felt motivated enough :) https://stackoverflow.com/help/self-answer
The parameter character.only can be used for that.
Example:
pckgs = c("readr", "dplyr")
sapply(pckgs, library, character.only = TRUE)

Error: could not find function "glimpse" [duplicate]

This question already has answers here:
Error: could not find function ... in R
(10 answers)
Closed 1 year ago.
I am trying to use glimpse function in R as below
df<- read.csv("movie-pang02.csv", stringsAsFactors = FALSE)
glimpse(df)
But I am getting the error:
Error: could not find function "glimpse"
Could anyone help me with it?
You'll need the dplyr package installed, first. Also, you need to have the glimpse call on a separate line.
library(dplyr)
glimpse(mtcars)
Install and load Package dplyr
library(dplyr)
First, You'll need the tibble package installed. Also, you need to have the glimpse call on a separate line
library(tibble)
glimpse(mpg)
None of the above solutions worked for me.
I was able to make it work by installing the pillar package :
install.packages("pillar")
Make sure that you run library(dplyr) first then run the glimpse() function.

Error with "function" class [duplicate]

This question already exists:
Error: Unused arguments for function aggregate()
Closed 9 years ago.
I'm trying to run the following script:
m <- matrix(c(1,1,2,1,3,12,14,16,30,21), nrow=5, ncol=2, byrow=FALSE);
colnames(m) <-c("Group","Score");
m<-data.frame(m)
head(m)
sum1 <- aggregate(list(total_score=m$Score), by=list(group=m$Group), FUN=sum)
sum1
But, when I run the script, the console returns the following error:
Error in as.data.frame.default(x) :
cannot coerce class '"function"' into a data.frame
I haven't seen this error before.... any thoughts from anyone as to what is wrong?
You most likely overwrote one of the built in functions, like list() or sum().
One thing to note, R automatically loads a workspace called ".RData" when it starts up, and you might have saved the previously overwritten functions in that file.
Check the folder you're starting R from for any .RData files, and either delete them or rename them(They might be hidden!), so R isn't loading anything on startup.

Name of a package for a given function in R [duplicate]

This question already has answers here:
Closed 10 years ago.
Possible Duplicate:
How do you you determine the namespace of a function?
I don't know how to do this... How do you know the package name for a certain function in R? I would like to have a function that given the name of a function, returns the name of the package that owns it. Any suggestion?
There may be better solutions, but find("functionname") seems to work reasonably well? However, it only works for loaded packages.
> find("strwidth")
[1] "package:graphics"
> find("qplot")
character(0)
> library(ggplot2)
> find("qplot")
[1] "package:ggplot2"
>
(If you need the raw name of the package you can use gsub("^package:","",results))
(The answers to the previous question linked by Andrie include this answer; they don't give the bit about gsub, and they all seem to share the issue of not finding non-loaded packages.)
Here's a quick hack to find functions even in non-loaded packages:
findAllFun <- function(f) {
h <- help.search(paste0("^",f,"$"),agrep=FALSE)
h$matches[,"Package"]
}
findAllFun("qplot")
## "ggplot2"
findAllFun("lambertW")
## "emdbook" "VGAM"
> findAllFun("xYplot")
## "Hmisc" "lattice"
If you need to find functions in non-installed packages (i.e. searching CRAN), then findFn from the sos package will be your friend.

Resources