dplyr - Evaluation error: argument "n" is missing, with no default - r

UPDATE: i uninstalled and reinstalled dplyr and now its working fine. Not sure why this happens
Often times when I'm using dplyr functions I get this error, "Evaluation error: argument "n" is missing, with no default."
It happens for random operations I use inside dplyr but I'll show a simple one I'm getting with the mutate function. Analyzing the years baseball players played in a df called hof, I have a "From" column which is their first year, and a "To" column which is their last year (neither column has any missing values and both are numeric). I'm using mutate to find their middle year
*Note: I'm sure there are other ways to do this without dplyr but I want to solve this issue since it happens to me often with filter as well
I've made sure commas, parenthesis, and all words are used correctly. I've also tried calling the function directly like dplyr::mutate
hof <- hof %>%
dplyr::mutate(MidCareer = (From + To) / 2)
And the error message
Error in mutate_impl(.data, dots) :
Evaluation error: argument "n" is missing, with no default.

Related

dplyr - No applicable method for 'count' applied to an object of class "character" using count() [duplicate]

My code was working and then RStudio crashed. When I opened it back up, a line of my code is not working now...
CodeTable<-count(unique(Data$Code))
Previously, this created a simple database of 3 columns: 1) Numeric order, 2)the unique Code, and 3) the count of each Code (which was always 1, since they were counted as unique). Now, I am receiving an error message:
Error in UseMethod("summarise_") :
no applicable method for 'summarise_' applied to an object of class "character"
I don't believe anything was changed from R crashing and I don't know how to now create my little 'count' table I had before.
count from dplyr requires input as a data.frame
library(dplyr)
Data %>%
count(Code)
In base R, we can do
table(Data$Code)

Why does pivot_longer() only work when I specify the dataframe inside the brackets, not with pipe notation?

I was trying to rearrange a dataframe using dataframe %>% pivot_longer( ) but would keep getting errors however I tried to specify the cols argument.
For instance, trying cols = (!colname) I'd get:
Error: no applicable method for 'pivot_longer' applied to an object of class "logical"
In addition: ‘!’ not meaningful for factors
Using starts_with("string") I'd get:
Error: starts_with() must be used within a selecting function.
(Even though the example given on the help page for usage of 'starts_with()' gives "The cols argument of tidyr::pivot_longer()" as an example of where it should be used!)
And trying cols = c(2:18) gave me:
no applicable method for 'pivot_longer' applied to an object of class "c('integer', 'numeric')
However, putting the name of the dataframe as the first argument within the brackets seems to have fixed the problem. I'm totally puzzled. I already tried library(tidyr) to check that no functions were being masked. Any idea what I could be doing wrong?

Count function for character class in R

My code was working and then RStudio crashed. When I opened it back up, a line of my code is not working now...
CodeTable<-count(unique(Data$Code))
Previously, this created a simple database of 3 columns: 1) Numeric order, 2)the unique Code, and 3) the count of each Code (which was always 1, since they were counted as unique). Now, I am receiving an error message:
Error in UseMethod("summarise_") :
no applicable method for 'summarise_' applied to an object of class "character"
I don't believe anything was changed from R crashing and I don't know how to now create my little 'count' table I had before.
count from dplyr requires input as a data.frame
library(dplyr)
Data %>%
count(Code)
In base R, we can do
table(Data$Code)

I get this error " Error in log2(exprs(gset)) : non-numeric argument to mathematical function"

The code is this:
gset <- ReadAffy()
pData(gset)
b1<-log2(exprs(gset))
boxplot(b1, col = 2:4)
str(exprs(gset))
List of 1
$ : symbol gset
At the third line, I get this error"Error in log2(exprs(gset)): non-numeric argument to mathematical function".
I used the same code and the same data a few days ago without any problem but now I don't know what's wrong. I haven't changed anything.
In line with the idea of conflicting packages highlighted above, a possible explanation can come from the fact that the exprs function in the dplyr package masks the exprs function in the Biobase package since both of functions have the same name in both packages.
https://support.bioconductor.org/p/109128/#109719
I have solved the problem in my case by placing exprs=Biobase::exprs at the top of my script.

dplyr rename command with spaces

I have tried multiple variations of the rename function in dplyr.
I have a data frame called from a database called alldata, and a column within the data frame named WindDirection:N. I am trying to rename it as Wind Direction. I understand creating variable names containing spaces is not a good practice, but I want it to be named as such to improve readability for a selectInput list in shiny, and even if I settle for renaming it WindDirection I am getting all of the same error messages.
I have tried:
rename(alldata, Wind Direction = WindDirection:N)
which gives the error message:
Error: unexpected symbol in "rename(alldata, Wind Direction"
rename(alldata, `Wind Direction` = `WindDirection:N`)
which does not give an error message, but also does not rename the variable
rename(alldata, "Wind Direction" = "WindDirection:N")
which gives the error message:
Error: Arguments to rename must be unquoted variable names. Arguments Wind Direction are not.
I then tried the same 3 combinations of the reverse order (because I know that is how plyr works even though I do not call it to be used using the library command earlier in my code) putting the old variable first and the new variable 2nd with similar error messages.
I then tried to specify the package as I have 1 example below and tried all 6 combinations again.
dplyr::rename(alldata, `Wind Direction` = `WindDirection:N`)
to similar error messages as the first time.
I have used the following thread as an attempt to do this myself.
Replacement for "rename" in dplyr
as agenis pointed out, my mistake was not redefining the dataframe after renaming the variable.
So where I had
dplyr::rename(alldata,Wind Direction=WindDirection:N)
I should have
alldata <- dplyr::rename(alldata,Wind Direction=WindDirection:N)

Resources