R: Error in Usemethod("Group_By_"):Applied to an Object of Class

Error in UseMethod(group_by_) : no applicable method for 'group_by_' applied to an object of class list

As the data frames appear to all have the same structure, you could bind them into one, then also group by the ID variable. I'm guessing your list names are just the longitude and latitude where your meteorological data was collected, so this shouldn't be a problem.

dataset %>% 
bind_rows(.id = "location") %>%
group_by(frame_name, year) %>%
summarize(Mean_Max_Temp = mean(Max.Temp), Mean_Min_Temp = mean(Min.temp),
Monthly_Precip = sum(Precipitation))

Alternatively, you could use map() to apply the same function to each element of your list.

dataset %>% 
map(~summarise(group_by(., Year),
Mean_Max_Temp = mean(Max.Temp),
Mean_Min_Temp = mean(Min.temp),
Monthly_Precip = sum(Precipitation)))

R: Error in UseMethod(group_by_) : applied to an object of class

In setting the class attribute to "LongitudinalData", you're telling R to use only methods for .LongitudinalData. Just like you how you've defined a subject.LongitudinalData that gets called when you execute subject(x, 14), R looks for group_by_.LongitudinalData when you call group_by_, but that, of course, doesn't exist because you just invented the class.

However, R has a simple inheritance-like feature so that you can specify backup classes to try if there is no method for the main class.

From ?class:

When a generic function fun is applied to an object with class attribute c("first", "second"), the system searches for a function called fun.first and, if it finds it, applies it to the object. If no such function is found, a function called fun.second is tried. If no class name produces a suitable function, the function fun.default is used (if it exists). If there is no class attribute, the implicit class is tried, then the default method.

Therefore, you can specify that your LongitudinalData object can also be treated like a data frame like so:

make_LD <- function(x){
structure(list(id = c(x$id), visit = c(x$visit),
room = c(x$room), value = c(x$value), timepoint = c(x$timepoint)),
class = c("LongitudinalData", "data.frame"))
}

However, there is some additional structure to a data frame that is missing, so it's usually better to create new classes by building upon an existing object rather than creating one from scratch:

make_LD <- function (x) {
class(x) <- c("LongitudinalData", class(x))
x
}

Note that there are several additional issues with your subject.LongitudinalData method that need to be corrected before it will function. I suggest reading vignette("programming", package = "dplyr")

Error in UseMethod(select) : no applicable method for 'select' applied to an object of class character

Two problems:

  • The first argument for your return_coef function is a data.frame named df1, yet you are calling it with df1$date2 (a string). I think you should instead start with

    mapply(return_coef, list(df1), df1$date2, df1$Category)

    (though this does error currently, see the next bullet).

    The list(df1) in this case means that the whole df1 will be passed as the first argument for each of the pairs from df1$date2 and df1$Category.

  • It now fails with argument "var1" is missing, with no default, but I suspect you were working towards that. I'll choose a couple of random names and ... something happens.

Ultimately, the function is fine as-is, just change your mapply use as:

mapply(return_coef, list(df1), df1$date2, df1$Category, var1 = "a1", var2 = "a2")
# [1] 6.539702 4.000000 1.000000 3.000000

Because both var1 and var2 are length-1, they are recycled for all calls to return_coef (as their named arguments).

Since you're using dplyr, this can be neatly put into a pipe a little more directly than using cbind(...):

library(dplyr)
df1 %>%
transmute(
date2, Category,
coef = mapply(return_coef, list(cur_data()), date2, Category, var1 = "a1", var2 = "a2")
)
# date2 Category coef
# 1 2021-06-27 ABC 6.539702
# 2 2021-07-01 ABC 4.000000
# 3 2021-07-02 ABC 1.000000
# 4 2021-07-03 ABC 3.000000

I use transmute instead of a preceding select(date2, Category) because the function needs variables present in the whole frame. I could easily have done mutate(coef=..) %>% select(date2, Category, coef) as well.

Can't apply group_by in Shiny

Try Market_Data <- Data %>% group_by(...)

Error in UseMethod(mutate) : no applicable method for 'mutate' applied to an object of class function when trying to seperate columns

The first argument to mutate must be a data.frame. You did not name your data.frame df, so the function df is passed to mutate.

args(df)
# function (x, df1, df2, ncp, log = FALSE)
# NULL

EDIT: After your update you added dput output of your data. Running your code gives me the error:

Survey %>%
mutate(Id = row_number(), HasAccount = "Yes") %>%
unnest_tokens(Network, `Which of these social media platforms do you have an account in right now?`, to_lower = F)
# Error in check_input(x) :
# Input must be a character vector of any length or a list of character
# vectors, each of which has a length of 1.

Your dput has the column named with underscores:

colnames(Survey)[5]
# "Which_of_these_social_media_platforms_do_you_have_an_account_in_right_now?"

Renaming the column:

Survey %>%
transmute(Id = row_number(), HasAccount = "Yes",
Platforms = `Which_of_these_social_media_platforms_do_you_have_an_account_in_right_now?`) %>%
unnest_tokens(Network, Platforms) %>%
spread(Network, HasAccount, fill = "No")
# # A tibble: 6 x 10
# Id facebook instagram linkedin quora reddit
# <int> <chr> <chr> <chr> <chr> <chr>
# 1 1 Yes Yes No No Yes
# 2 2 No No No No Yes
# 3 3 Yes Yes Yes Yes Yes
# 4 4 Yes Yes No No No
# 5 5 Yes Yes No No No
# 6 6 Yes Yes Yes No No
# # … with 4 more variables: signal <chr>,
# # snapchat <chr>, tiktok <chr>, twitter <chr>


Related Topics



Leave a reply



Submit