Setting names of new calculated variables and assign values using a function - r
I am writing a function, which is using references from two data sets (RawDataSplit and Hierarchy). All these datasets are connected. I need to summarise the data based on a function that will be applied to all the selected columns. I am using summarise_ to do this.
I have created a function which will be used to summarise the columns per_qid, this is to calculate percentage for each column.
Then I am creating a function with three arguments: fav_fun(dataset,levelname,demo_var)
dataset is for the data on which we need aggregations, in this case it is RawDataSplit
levelname is one of the column in RawDataSplit by which data would be grouped
demo_var would be one of the variable chosen from data set 'Hierarchy', this would be one of the demographic variable like Age.
Function for aggregation:
per_qid<-function(x){
counts<-sum(ifelse(x==4|x==5,1,0),na.rm=TRUE
total<-sum(ifelse(x==1|x==2|x==3|x==4|x==5,1,0),na.rm=TRUE)
#Masking and rounding
x<- ifelse(total<masking_criteria,NA,roundUp(counts*100/total)) )
x
}
Function to aggregate RawDataSplit file:
fav_fun<-function(dataset,levelname,demo_var){
demo_var<- enquo(demo_var)
levelname<-enquo(levelname)
demographic<-Hierarchy[,demo_var]
demo<-levels(demographic)
summ_method<- paste('per_qid(',questions,')')
summ_name<-paste0(questions,'_',demographic)
fav <- dataset %>%
group_by(!!levelname) %>%
filter(status=="Complete") %>%
filter(!!demo_var==demo[1]) %>%
summarise_(.dots = setNames,(summ_method,summ_name))
fav
}
this fav_fun is throwing different kind of errors sometimes for summarise_ and sometimes for other variables. Maybe something is wrong with the quoting and enquoting of variable also.
The sample data for these datasets are:
RawDataSplit:
structure(list(QID_1 = c(4L, 3L, 5L, 2L, 5L, 5L), QID_2 = c(4L,
3L, 5L, 2L, 5L, 5L), QID_3 = c(4L, 4L, 4L, 6L, 5L, 5L), mastercode_1 = c(NA,
NA, NA, NA, NA, NA), mastercode_2 = structure(c(1L, 1L, 1L, 1L,
1L, 1L), .Label = "BROLLAND", class = "factor")), row.names = c(NA,
-6L), class = c("data.table", "data.frame"), .internal.selfref = <pointer: 0x00372498>)
questions:
c("QID_1", "QID_10", "QID_11")
Hierarchy : (for now I have included one column only which is Age, I need to use it as demo_var in the function)
structure(c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NA, 3L, NA, 2L, 5L, 3L, NA, 4L, 2L, 3L, 2L, 2L, 2L,
2L, NA, NA, NA, NA, NA, 4L, 5L, NA, 3L, 4L, 3L, 2L, 3L, 3L, 3L,
3L, 3L, 4L, 3L, NA, 3L, 4L, NA, 3L, 3L, 4L, NA, NA, 4L, NA, 3L,
NA, 4L, 3L, 3L, NA, NA, 3L, NA, NA, 2L, NA, 2L, 3L, 4L, 5L, 3L,
4L, NA, NA, 2L, 4L, 2L, 2L, 2L, 4L, 2L, NA, NA, 4L, NA, NA, 4L,
3L, 3L, 2L, 2L, NA, 2L, 4L, 4L, NA, 4L, NA, 3L, 3L, 1L, 4L, NA,
NA, 3L, 2L, 2L, NA, 2L, 5L, 4L, 2L, NA, NA, 2L, 5L, NA, NA, 3L,
3L, 1L, NA, 2L, 2L, 5L, NA, 4L, 3L, 4L, 2L, 2L, NA, 2L, NA, 4L,
NA, 4L, 2L, 4L, NA, 2L, NA, 3L, 4L, 4L, 2L, NA, NA, NA, 2L, 3L,
4L, 4L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 4L, 3L, 3L, 2L, 3L, 4L, NA,
4L, 3L, 4L, 2L, 4L, 4L, 4L, 2L, NA, 3L, NA, 3L, NA, 3L, 4L, 4L,
3L, 4L, 3L, 2L, 4L, 5L, 3L, 3L, NA, 3L, NA, NA, NA, NA, 2L, NA,
NA, NA, NA, NA, NA, 3L, NA, 2L, NA, 4L, NA, 3L, NA, NA, 3L, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 4L, NA,
2L, NA, NA, NA, NA, 3L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 3L, NA, NA, NA, NA, 2L, NA, 2L, 2L, NA, 3L, NA, NA, NA, NA,
NA, NA, NA, 2L, NA, 2L, 3L, NA, 2L, NA, 2L, 3L, 3L, NA, 2L, 4L,
3L, NA, NA, NA, NA, NA, NA, NA, 2L, NA, NA, NA, NA, NA, NA, NA,
3L, NA, NA, NA, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 2L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 2L, NA, NA, 2L, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
2L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2L, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 3L,
NA, 3L, 3L, 2L, 2L, NA, 3L, 1L, NA, NA, NA, 4L, 2L, NA, NA, NA,
NA, NA, 5L, NA, NA, NA, NA, 2L, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, 4L, NA, NA, NA, NA, NA, NA, NA, 5L, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 2L, NA, NA, NA, NA, 2L, NA, NA, NA, 2L, NA, NA,
NA, NA, 3L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 2L, 4L, NA,
NA, NA, NA, NA, 2L, 3L, 2L, 5L, NA, 2L, NA, 1L, 2L, NA, NA, 2L,
4L, 4L, NA, 3L, 3L, NA, 3L, 3L, NA, NA, NA, 4L, 4L, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
3L, NA, NA, 2L, NA, NA, NA, NA, 3L, 3L, NA, NA, NA, NA, NA, 3L,
NA, 3L, 2L, 2L, 2L, 3L, 4L, 2L, 3L, 4L, 2L, 2L, 3L, 3L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, 1L, 2L, 5L, 2L, 2L, 2L, 2L, 3L, 2L, 3L,
2L, 3L, 4L, 4L, 3L, 4L, 3L, 4L, 4L, 3L, 2L, 2L, 2L, 4L, 4L, 2L,
3L, 4L,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, 3L, 3L, 3L, 2L, 3L, 3L, 2L, 2L, NA, 2L, 2L, 2L,
3L, 2L, 2L, 2L, 1L, NA, 3L, 3L, NA, 3L, 2L, 4L, 3L, 3L, 2L, 3L,
3L, 2L, 3L, 3L, 2L, 3L, NA, 3L, 3L, 4L, 2L, 3L, 4L, 4L, 3L, 3L,
3L, 3L, 3L, 3L, 2L, 4L, 4L, 3L, 4L, 3L, 3L, 3L, 2L, NA, 2L, 2L,
3L, 3L, 2L, 4L, 3L, 4L, 4L, 4L, 3L, 3L, 2L, 3L, 2L, 3L, 3L, 4L,
3L, 3L, 5L, 3L, 2L, 4L, 4L, 3L, 5L, 3L, 3L, 4L, 3L, NA, 3L, 1L,
2L, 3L, 1L, 1L, 1L, 2L, 3L, 2L, 3L, 4L, 4L, 2L, 3L, 4L, 2L, 3L,
2L, 4L, NA, 3L, 2L, 3L, 3L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 2L,
3L, 3L, NA, NA, NA, NA, NA, 3L, NA, 3L, NA, NA, NA, NA, 3L, NA,
4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 4L, 3L, NA, 5L, 5L, NA,
3L, NA, NA, NA, NA, NA, NA, NA, 3L, NA, 4L, 3L, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 5L, NA, NA, 4L, 4L, NA,
NA, NA, NA, NA, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
4L, NA, NA, 3L, 3L, NA, 3L, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, 4L, NA, NA, NA, NA, 4L, 4L, 3L, 3L, NA, NA, NA, NA, 3L, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, 4L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
3L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, 4L, NA, NA, NA, NA, 3L, NA, 3L, NA, 4L, 3L, NA, NA,
NA, 4L, NA, 3L, NA, NA, NA, 4L, 3L, NA, NA, 3L, 3L, NA, NA, NA,
NA, NA, NA, 3L, NA, NA, 4L, NA, 5L, NA, NA, 4L, NA, NA, NA, NA,
NA, 5L, 4L, NA, 5L, NA, 2L, 2L, NA, NA, NA, NA, NA, NA, NA, NA,
5L, NA, NA, NA, NA, NA, NA, 5L, NA, NA, NA, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 5L, NA, 3L, 2L, NA, 5L, NA, NA, NA, NA, NA, NA,
NA, 2L, 2L, NA, 3L, NA, 4L, NA, 4L, 5L, 4L, 4L, NA, NA, 4L, NA,
NA, 5L, 4L, NA, NA, 3L, 5L, NA, 5L, NA, NA, NA, 3L, 3L, 5L, 5L,
3L, 3L, NA, 2L, 1L, 5L, 5L, NA, 4L, 5L, 5L, NA, NA, NA, NA, NA,
NA, NA, NA, NA, 5L, 1L, 2L, 4L, 5L, 1L, NA, NA, 3L, NA, NA, NA,
4L, NA, NA, NA, 4L, 4L, NA, NA, 5L, 4L, 4L, NA, NA, 4L, 4L, 3L,
4L, 3L, 2L, 2L, NA, 3L, 3L, 3L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L, 2L, 2L, NA, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 3L,
3L, 2L, NA, 3L, 2L, 3L, 3L, 4L, 3L, 3L, 4L, 3L, 4L, 4L, 3L, 2L,
2L, 3L, 3L, 3L, 2L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 2L, 3L, 4L, 3L, 3L, 3L, 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, 5L, 5L, 4L, 5L, 2L, 2L, 3L, 4L, 3L, 3L, 3L, 2L, 2L,
4L, 3L, 2L, 2L, 4L, 5L, 4L, 1L, 1L, 2L, 1L, 2L, 4L, 2L, 2L, 5L,
NA, 3L, 2L, 1L, 4L, 4L, 3L, 3L, 2L, 2L, 1L, 3L, 3L, NA, 3L, 3L,
3L, 4L, 3L, 4L, 1L, 2L, 4L, 3L, 4L, 1L, 2L, 2L, 4L, 2L, 2L, 3L,
2L, 5L, 5L, 3L, 5L, NA, 3L, NA, 3L, 5L, 4L, 4L, 5L, 3L, 3L, 3L,
4L, 3L, 3L, 4L, 5L, 4L, 4L, 4L, NA, NA, 4L, NA, 4L, 5L, 4L, 3L,
5L, 3L, 4L, 3L, 4L, NA, 3L, NA, 3L, 3L, NA, NA, 4L, 4L, NA, NA,
NA, NA, NA, NA),
.Label = c("18 to less than 25 years old", "25 to less than 35 years old",
"35 to less than 45 years old", "45 to less than 55 years old",
"More than 55 years old"), class = "factor")
I need a function fav_fun which takes these three arguments and aggregates the RawDataSplit and modifies names too. Like for QID_1 the new name after aggregation would be QID_1_45 to less than 55 years old for level 1 of Age variable
function will be called as: fav_fun(RawDataSplit,mastercode_2,Age)
Related
How to set placeholders in R
df1 <- structure(c(3L, NA, 3L, 3L, 3L, 2L, 3L, 2L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 1L, 3L, 1L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 1L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 1L, 3L, 2L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 2L, 3L, 3L, 1L, 3L, 2L, 2L, 3L, 2L, 3L, 1L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 1L, 2L), levels = c("aaa", "bbb", "ccc"), class = c("ordered", "factor")) df2 <- structure(c(1L, NA, 3L, 1L, 1L, 3L, 2L, 1L, 3L, 2L, 2L, 3L, 1L, 1L, 2L, 3L, 1L, 3L, 2L, 2L, 1L, 3L, 2L, 3L, 2L, 2L, 2L, 3L, 3L, 1L, 2L, 1L, 3L, 2L, 3L, 1L, 2L, 3L, 2L, 3L, 2L, 1L, 3L, 3L, 3L, 2L, 2L, 1L, 3L, 2L, 1L, 2L, 3L, 2L, 2L, 3L, 2L, 2L, 3L, 2L, 3L, 1L, 2L, 2L, 1L, 2L, 1L, 1L, 2L, 3L, 3L, 3L, 1L, 2L, 1L, 2L, 2L, 3L, 3L, 1L, 3L, 3L), levels = c("aaa", "bbb", "ccc"), class = c("ordered", "factor")) df3 <- structure(c(3L, 2L, 2L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 2L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 2L, 2L, 3L, 3L, 2L, 2L, 3L, 1L, 3L, 2L, 2L, 3L, 3L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 2L, 3L, 1L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 3L, 3L), levels = c("ddd", "eee", "fff" ), class = c("ordered", "factor")) dftest1 <- structure(c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, 1L, NA, NA, NA, 2L, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, 2L), levels = c("AAA", "BBB" ), class = "factor") dftest2 <- structure(c(NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, 1L, NA, 1L, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, 1L, 1L, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, 1L, NA, NA, NA, 1L, NA, 1L, NA, 1L, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, 1L, 1L, 1L), levels = "CCC", class = "factor") I want to put df1, df2 and df3 (in my case factors) together as placeholders vars2use, so to speak. var2use <- c(var1, var2, var3) This placeholder should then be combined with other factors (dftest1, dftest2) into a data set. This implementation works as expected: df <- data.frame(dftest1, dftest2, df1, df2, df3) I was hoping that it could also be implemented in this form: df <- data.frame(dftest1, dftest2, var2use) But I get an error: Error in data.frame(dftest1, dftest2, var2use): arguments imply differing number of rows: 82, 3 The background is that I would like to work with placeholders of this type in different places. Does anyone have an idea how to solve this?
The more direct way to do this is to just put your values in to a separte data.frame and then cbind() the values when you need them. For example vars2use <- data.frame(df1, df2, df3) df <- cbind(data.frame(dftest1, dftest2), vars2use) # df <- data.frame(dftest1, dftest2) |> cbind(vars2use) # alternative syntax If for some reason those names really do need to be a character vector, you can use mget() to get the values before using cbind varnames <- c("df1", "df2", "df3") df <- cbind(data.frame(dftest1, dftest2), mget(varnames))
Using nest_by or nest, group_by or looping to perform several chi square test to several variables in R (likert scales)
Let's say I have these categorical variables in my data set. All variables are related to the people's concerns of the COVID-19 and were assessed two times (with different participants..). And my main goal is to check if time (will be "constant") is associated with the prevalence of each item (economy, social cohesion, and so on) (will vary). Therefore, I'll need to perform several Chi-square tests. I've followed some instructions using nest_by or xtabs , but I'm not getting the right results. I would like to keep the tidyverse environment in this analysis. The main goal is to have several chi-squared tests, such as this one: ds_plot_likert %>% pivot_longer(cols = -c(time), names_to = "item", values_to = "response") %>% group_by(item, time, response) %>% summarise(N = n()) %>% mutate(pct = N / sum(N)) %>% filter(item == "Children's academic achievement") %>% #need to change all the time... xtabs(formula = pct ~ time + response, data = .) %>% chisq.test() But for all variables in my dataset (and preferably using tidyverse). Thank you! The following code gives you the possibility to reproduce. ds <- structure(list(time = c("First", "First", "First", "First", "First", "First", "First", "First", "First", "First", "First", "First", ".Second", "First", "First", "First", "First", ".Second", "First", "First", "First", "First", "First", "First", "First", "First", ".Second", "First", "First", ".Second", "First", "First", "First", "First", "First", "First", "First", "First", "First", "First", ".Second", "First", "First", "First", ".Second", ".Second", "First", "First", "First", ".Second", ".Second", "First", "First", "First", "First", ".Second", ".Second", "First", "First", "First", "First", "First", ".Second", "First", "First", "First", "First", ".Second", "First", "First", "First", "First", "First", "First", "First", ".Second", "First", ".Second", "First", "First", "First", "First", "First", "First", "First", "First", "First", "First", "First", ".Second", "First", "First", "First", "First", "First", "First", "First", ".Second", ".Second", "First"), Economy = structure(c(4L, 3L, 3L, 4L, 3L, 4L, 4L, 1L, 3L, 3L, 3L, 3L, 3L, 4L, 3L, 4L, 4L, 3L, 3L, NA, 2L, 3L, 4L, 3L, 3L, 4L, 4L, 2L, 3L, 4L, 4L, 3L, 2L, 4L, 2L, 3L, 2L, 3L, 3L, 3L, 2L, 4L, 4L, 3L, 3L, 3L, 4L, 3L, 3L, 4L, 2L, 4L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 3L, 4L, 3L, 2L, 3L, 3L, 3L, 4L, NA, 2L, 4L, 3L, 4L, 2L, 3L, 3L, 2L, NA, 3L, 2L, 3L, 2L, 3L, 4L, 3L, 3L, 3L, 3L, 3L, 4L, 3L, 4L, 3L, 3L, 2L, 3L, 3L, 3L, 4L), .Label = c("Not at all", "A little", "Moderately", "Very much"), class = "factor"), `My personal finance` = structure(c(3L, 2L, 4L, 2L, 4L, 4L, 3L, 2L, 3L, 3L, 2L, 3L, 2L, 4L, 3L, 4L, 4L, 2L, 3L, NA, 3L, 3L, 4L, 3L, 3L, 4L, 3L, 4L, 3L, 4L, 4L, 3L, 4L, 3L, 4L, 2L, 2L, 3L, 2L, 2L, 4L, 3L, 4L, 3L, 3L, 3L, 3L, 2L, 2L, 3L, 2L, 4L, 2L, 3L, 3L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 4L, 4L, 2L, 3L, 3L, 4L, 3L, NA, 2L, 3L, 3L, 4L, 2L, 3L, 2L, 3L, NA, 3L, 2L, 2L, 2L, 3L, 4L, 3L, 2L, 3L, 2L, 2L, 2L, 2L, 4L, 3L, 2L, 2L, 3L, 3L, 3L, 3L), .Label = c("Not at all", "A little", "Moderately", "Very much"), class = "factor"), `My own health` = structure(c(3L, 2L, 4L, 3L, 4L, 4L, 3L, 4L, 2L, 4L, 3L, 3L, 3L, 4L, 3L, 3L, 4L, 3L, 2L, NA, 3L, 4L, 3L, 4L, 3L, 2L, 1L, 2L, 3L, 3L, 4L, 2L, 4L, 2L, 4L, 4L, 3L, 2L, 2L, 4L, 4L, 2L, 4L, 3L, 3L, 2L, 3L, 3L, 2L, 2L, 3L, 3L, 1L, 3L, 4L, 4L, 3L, 3L, 2L, 2L, 4L, 3L, 3L, 4L, 2L, 3L, 3L, 4L, 4L, NA, 2L, 2L, 3L, 4L, 1L, 3L, 4L, 3L, NA, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 2L, 4L, 2L, 2L, 3L, 4L, 3L, 2L, 3L, 4L, 2L, 3L, 3L), .Label = c("Not at all", "A little", "Moderately", "Very much"), class = "factor"), `My friends and family health` = structure(c(4L, 3L, 4L, 4L, 4L, 4L, 3L, 4L, 3L, 4L, 3L, NA, 3L, 4L, 3L, 3L, 4L, 4L, 3L, NA, 3L, 4L, 3L, 4L, 3L, 3L, 2L, 2L, 3L, 4L, 4L, 2L, 4L, 1L, 4L, 4L, 4L, 4L, 3L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L, 4L, 3L, 4L, 3L, 3L, 4L, 3L, 3L, 4L, 3L, 3L, 3L, 2L, 4L, 3L, 4L, 4L, 2L, 3L, 3L, 4L, 4L, NA, 2L, 4L, 3L, 4L, 2L, 3L, 4L, 3L, NA, 3L, 4L, 3L, 3L, 4L, 3L, 4L, 4L, 3L, 4L, 3L, 3L, 3L, 3L, 4L, 3L, 3L, 4L, 4L, 3L, 4L), .Label = c("Not at all", "A little", "Moderately", "Very much"), class = "factor"), `Social cohesion` = structure(c(3L, 3L, 2L, 4L, 4L, 2L, 4L, 4L, 2L, 3L, 3L, 3L, 3L, 4L, 3L, 3L, 4L, 3L, 3L, NA, 3L, 3L, 4L, 3L, 3L, 2L, 1L, 2L, 2L, 4L, 4L, 3L, 3L, 1L, 3L, NA, 2L, 3L, 2L, 4L, 4L, 2L, 3L, 3L, 4L, 4L, 3L, 3L, 3L, 2L, 2L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 4L, 2L, 3L, 3L, 3L, 4L, NA, 2L, 3L, 3L, 2L, 1L, 1L, 3L, 2L, NA, 3L, NA, 3L, 3L, 4L, 2L, 4L, 3L, 1L, 4L, 2L, 4L, 3L, 2L, 4L, 2L, 3L, 4L, 4L, 2L, 2L), .Label = c("Not at all", "A little", "Moderately", "Very much"), class = "factor"), `Food and pharmaceutical drugs` = structure(c(2L, 3L, 4L, 4L, 3L, 2L, 4L, 1L, 2L, 4L, 3L, NA, 2L, 3L, 3L, 2L, 4L, 3L, 3L, NA, 3L, 4L, 3L, 3L, 3L, 1L, 1L, 2L, 3L, 4L, 2L, 2L, 4L, 4L, 4L, 1L, 4L, 2L, 2L, 3L, 4L, 2L, 4L, 3L, 2L, 2L, 3L, 3L, 2L, 3L, 2L, 3L, 4L, 2L, 2L, 2L, 3L, 3L, 3L, 2L, 4L, 3L, 3L, 4L, 2L, 3L, 3L, 3L, 4L, NA, 2L, 3L, 2L, 2L, 1L, 1L, 2L, 2L, NA, 1L, 1L, 2L, 2L, 2L, 1L, 3L, 3L, 2L, 3L, 1L, 2L, 2L, 2L, 4L, 2L, 3L, 3L, 2L, 1L, 2L), .Label = c("Not at all", "A little", "Moderately", "Very much"), class = "factor"), `Price of grocery products` = structure(c(2L, 2L, 3L, 4L, 3L, 4L, 3L, 2L, 2L, 4L, 3L, 3L, 2L, 4L, 3L, 3L, 4L, 4L, 3L, NA, 3L, 4L, 3L, 3L, 3L, 2L, 3L, 2L, 3L, 4L, 2L, 4L, 3L, 4L, 4L, 4L, 4L, 2L, 2L, 3L, 4L, 2L, 4L, 3L, 3L, 3L, 3L, 4L, 2L, 4L, 2L, 3L, 4L, 2L, 2L, 3L, 4L, 3L, 2L, 3L, 4L, 3L, 4L, 4L, 2L, 3L, 3L, 4L, 4L, NA, 2L, 3L, 2L, 2L, 2L, 1L, 3L, 3L, NA, 1L, 1L, 2L, 2L, 3L, 1L, 3L, 4L, 2L, 3L, 2L, 2L, 2L, 2L, 4L, 3L, 3L, 3L, 4L, 1L, 2L), .Label = c("Not at all", "A little", "Moderately", "Very much"), class = "factor"), `Stock prices` = structure(c(2L, 2L, 2L, 4L, 3L, 2L, 1L, 1L, 3L, 4L, 2L, 2L, 2L, 4L, 3L, 3L, 4L, 3L, 2L, NA, 1L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 3L, 1L, 2L, 2L, 2L, 4L, 4L, 2L, 3L, 2L, 2L, 3L, 2L, 3L, 4L, 3L, 4L, 3L, 3L, 2L, 2L, 2L, 1L, 3L, 3L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, NA, 2L, 4L, 2L, 3L, 3L, 4L, 4L, NA, 2L, 2L, 2L, 2L, 2L, 1L, 3L, 3L, NA, 3L, 1L, 3L, 3L, 4L, 1L, 3L, 4L, 1L, 3L, 1L, 4L, 2L, 2L, 2L, NA, 3L, 2L, 4L, 1L, 2L), .Label = c("Not at all", "A little", "Moderately", "Very much"), class = "factor"), `Children's academic achievement` = structure(c(4L, 3L, 4L, 1L, NA, NA, 1L, 1L, 1L, 1L, 3L, 3L, 2L, 4L, 3L, 4L, NA, 4L, 3L, NA, 1L, 1L, 3L, 3L, 3L, 1L, 4L, 1L, 3L, 4L, 3L, 2L, 4L, 1L, 4L, 1L, 1L, 3L, 2L, 3L, 1L, 2L, 1L, 3L, 2L, 3L, 3L, 3L, 1L, 1L, 1L, 4L, 1L, 1L, 2L, 3L, 3L, 3L, 3L, 2L, 4L, 3L, 3L, 4L, 2L, 2L, 1L, 1L, 4L, NA, 2L, 2L, 2L, 4L, 1L, 2L, 1L, 2L, NA, 2L, 1L, NA, 3L, 2L, 2L, 1L, 4L, 2L, 3L, 1L, 4L, 1L, 1L, 1L, 3L, 1L, 1L, 1L, 1L, 2L), .Label = c("Not at all", "A little", "Moderately", "Very much"), class = "factor")), class = "data.frame", row.names = c(NA, -100L))
You can store the result in a list for each item - library(dplyr) library(tidyr) ds %>% pivot_longer(cols = -c(time), names_to = "item", values_to = "response") %>% group_by(item, time, response) %>% summarise(N = n()) %>% mutate(pct = N / sum(N)) %>% group_by(item) %>% summarise(test = list(xtabs(formula = pct ~ time + response, data = cur_data()) %>% chisq.test())) -> result result$test #[[1]] # Pearson's Chi-squared test #data: . #X-squared = 0.0099329, df = 3, p-value = 0.9997 #[[2]] # # Pearson's Chi-squared test #data: . #X-squared = 0.026631, df = 3, p-value = 0.9989 #... #...
How to perform multiple t.tests with R package srvyr?
As a follow up on a recent SO question (see here) I am wondering how to perform multiple t.tests in R with weighted data (package srvyr). I cant make it run and would be happy if anyone could help me here. I added a random sample in the code below. Many thanks! #create data surveydata <- as.data.frame(replicate(1,sample(1:5,1000,rep=TRUE))) colnames(surveydata)[1] <- "q1" surveydata$q2 <- sample(6, size = nrow(surveydata), replace = TRUE) surveydata$q3 <- sample(6, size = nrow(surveydata), replace = TRUE) surveydata$q4 <- sample(6, size = nrow(surveydata), replace = TRUE) surveydata$group <- c(1,2) #replace all value "6" wir NA surveydata[surveydata == 6] <- NA #add NAs to group 1 in q1 surveydata$q1[which(surveydata$q1==1 & surveydata$group==1)] = NA surveydata$q1[which(surveydata$q1==2 & surveydata$group==1)] = NA surveydata$q1[which(surveydata$q1==3 & surveydata$group==1)] = NA surveydata$q1[which(surveydata$q1==4 & surveydata$group==1)] = NA surveydata$q1[which(surveydata$q1==5 & surveydata$group==1)] = NA #add weights surveydata$weights <- round(runif(nrow(surveydata), min=0.2, max=1.5), 3) #create vector for relevant questions rquest <- names(surveydata)[1:4] # create survey design library(srvyr) surveydesign <- surveydata %>% as_survey_design(strata = group, weights = weights, variables = c("group", all_of(rquest))) # perform multiple t.test (doesn't work yet) outcome <- do.call(rbind, lapply(names(surveydesign$variables)[-1], function(i) { tryCatch({ test <- t.test(as.formula(paste(i, "~ survey")), data = surveydesign) data.frame(question = i, group1 = test$estimate[1], group2 = test$estimate[2], difference = diff(test$estimate), p_value = test$p.value, row.names = 1) }, error = function(e) { data.frame(question = i, group1 = NA, group2 = NA, difference = NA, p_value = NA, row.names = 1) }) }))
As I understand it you have a series of question columns in the example q1 to q4. You've used srvyr to generate a weights column. It is possible in our data that for a particular question one entire group maybe all NA and you'd like to generate results into a df even when that is true. You want a weighted Student's t-test making use of the weights column not a simple t-test. The only function I found that provides that is weights::wtd.t.test which doesn't offer a formula interface but wants to be fed vectors. In order of steps taken: Load requisite libraries library(srvyr) library(dplyr) library(rlang) library(weights) Build a custom function that removes the NAs by variable, pulls the vectors for x, y, weightx, weighty, runs the test, and extracts the info you want into a df row. multiple_wt_ttest <- function(i) { i <- ensym(i) xxx <- surveydata %>% filter(!is.na(!!i)) %>% split(.$group) newx <- pull(xxx[[1]], i) newy <- pull(xxx[[2]], i) wtx <- pull(xxx[[1]], weights) wty <- pull(xxx[[2]], weights) test <- wtd.t.test(x = newx, y = newy, weight = wtx, weighty = wty, samedata = FALSE) data.frame(question = rlang::as_name(i), group1 = test$additional[[2]], group2 = test$additional[[3]], difference = test$additional[[1]], p.value = test$coefficients[[3]]) } Once we have the function we can use lapply to apply it column by column (notice it handles the case in q2 where group == 1 is all NA. lapply(names(surveydata)[1:4], multiple_wt_ttest) #> [[1]] #> question group1 group2 difference p.value #> 1 q1 NaN 3.010457 NaN NA #> #> [[2]] #> question group1 group2 difference p.value #> 1 q2 3.009003 3.071842 -0.06283922 0.515789 #> #> [[3]] #> question group1 group2 difference p.value #> 1 q3 2.985096 2.968867 0.0162288 0.8734034 #> #> [[4]] #> question group1 group2 difference p.value #> 1 q4 2.856255 3.047787 -0.1915322 0.04290471 Finally, wrap it in a do.call and rbind to make the df you desire do.call(rbind, lapply(names(surveydata)[1:4], multiple_wt_ttest)) #> question group1 group2 difference p.value #> 1 q1 NaN 3.010457 NaN NA #> 2 q2 3.009003 3.071842 -0.06283922 0.51578905 #> 3 q3 2.985096 2.968867 0.01622880 0.87340343 #> 4 q4 2.856255 3.047787 -0.19153218 0.04290471 Your data (without showing all the gyrations to create it and heading the first 200 rows) surveydata <- structure(list(q1 = c(NA, 1L, NA, 4L, NA, 5L, NA, 3L, NA, 5L, NA, 5L, NA, 1L, NA, 5L, NA, 3L, NA, 4L, NA, 5L, NA, 4L, NA, 2L, NA, 5L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 2L, NA, 5L, NA, 4L, NA, 4L, NA, 3L, NA, 4L, NA, 2L, NA, 4L, NA, 3L, NA, 1L, NA, 1L, NA, 3L, NA, 5L, NA, 3L, NA, 5L, NA, 5L, NA, 4L, NA, 2L, NA, 5L, NA, 1L, NA, 3L, NA, 2L, NA, 5L, NA, 4L, NA, 1L, NA, 5L, NA, 2L, NA, 2L, NA, 4L, NA, 1L, NA, 3L, NA, 4L, NA, 5L, NA, 3L, NA, 5L, NA, 1L, NA, 1L, NA, 3L, NA, 2L, NA, 4L, NA, 4L, NA, 1L, NA, 4L, NA, 3L, NA, 2L, NA, 3L, NA, 5L, NA, 2L, NA, 5L, NA, 2L, NA, 1L, NA, 5L, NA, 2L, NA, 1L, NA, 2L, NA, 3L, NA, 2L, NA, 3L, NA, 4L, NA, 4L, NA, 3L, NA, 1L, NA, 3L, NA, 1L, NA, 5L, NA, 3L, NA, 5L, NA, 4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 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L), .Label = c("1", "2"), class = "factor"), weights = c(1.445, 0.408, 0.621, 0.961, 1.492, 0.625, 1.131, 0.246, 0.612, 0.621, 1.311, 0.649, 1.282, 0.898, 1.268, 0.641, 0.764, 0.759, 0.306, 0.707, 0.899, 0.785, 1.279, 0.458, 0.882, 0.384, 1.492, 0.468, 0.785, 0.707, 0.489, 1.113, 0.692, 0.293, 0.642, 1.327, 0.362, 1.405, 1.173, 0.732, 0.661, 0.522, 1.001, 0.374, 1.181, 0.819, 1.389, 0.43, 0.477, 0.879, 0.634, 0.417, 0.359, 1.007, 0.866, 0.203, 1.469, 0.294, 1.326, 1.391, 0.871, 1.036, 1.251, 0.417, 1.074, 1.268, 0.963, 0.469, 0.215, 1.074, 0.644, 1.054, 0.787, 0.714, 0.568, 0.397, 1.421, 0.692, 0.262, 0.644, 0.793, 0.808, 0.25, 0.842, 1.039, 0.359, 0.987, 1.257, 0.301, 0.203, 0.823, 1.328, 1.192, 0.256, 1.099, 0.668, 1.129, 0.413, 0.266, 1.121, 0.893, 1.484, 0.568, 1.255, 0.531, 0.461, 0.773, 0.298, 0.233, 0.676, 0.478, 0.806, 0.556, 0.201, 0.801, 0.348, 1.396, 0.552, 0.384, 0.615, 0.499, 0.819, 0.954, 0.943, 0.956, 0.323, 0.706, 0.699, 0.9, 1.156, 1.436, 1.115, 0.762, 0.258, 1.421, 0.644, 1.349, 0.251, 0.735, 0.479, 1.055, 1.395, 1.062, 1.155, 0.869, 0.436, 0.415, 0.745, 1.247, 0.21, 0.879, 0.776, 0.747, 0.835, 0.609, 0.733, 0.563, 1.067, 1.436, 0.679, 1.497, 1.385, 1.087, 1.286, 0.503, 0.738, 0.504, 0.665, 1.421, 1.288, 0.691, 0.972, 0.467, 0.425, 0.406, 0.862, 0.749, 0.935, 0.291, 0.444, 1.118, 1.048, 0.886, 0.982, 0.578, 1.402, 0.778, 1.139, 0.804, 0.618, 1.147, 0.594, 0.984, 0.986, 0.941, 0.794, 0.323, 1.41, 0.902, 0.417)), row.names = c(NA, 200L), class = "data.frame")
error bars should not be very long in barplots in r
I am plotting grouped barplots with error bars, but my error bars are very long as in this image [![https://i.stack.imgur.com/VUByO.png][1]][1]. I would like shorter error bars as in this image [![https://i.stack.imgur.com/JhaUJ.png][2]][2] The code used per$Leaf_Location <- factor(per$Leaf_Location, levels = unique(per$Leaf_Location)) per$Time <- factor(per$Time, levels = unique(per$Time)) ggplot(per, aes(x=Leaf_Location, y=Damage, fill=as.factor(Time))) + stat_summary(fun.y=mean, geom="bar",position=position_dodge(),colour="black",width=.7,size=.7) + stat_summary(fun.ymin=min,fun.ymax=max,geom="errorbar", color="black",position=position_dodge(.7), width=.2) + stat_summary(geom = 'text', fun.y = max, position = position_dodge(.7), label = c("a","b","c","d","d","a","b","c","d","d","a","b","c","d","d"), vjust = -0.5) + scale_fill_manual("Legend", values = c("grey36","grey46","grey56","grey76","grey86","grey96")) + xlab("Leaf Location") + ylab("Damage ") + theme_bw() data: per = structure(list(Site = structure(c(2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L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L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L, 1L, 1L, 1L, 2L, 2L, 2L, 3L, 3L, 3L, 4L, 4L, 4L, 5L, 5L, 5L), .Label = c("20_days", "40_days", "60_days", "80_days", "100_days"), class = "factor"), Damage = c(25.25, 26.07, 24.43, 20.73, 17.8, 6.9, 45.05, 33.47, 24.43, 51.67, 41.72, 34.17, 81.67, 73.33, 55.83, 34.28, 26.08, 13.28, 26.27, 14.1, 6.93, 37.55, 29.33, 23.62, 49.17, 38.45, 31.38, 70.83, 60.83, 44.2, 31.03, 25.2, 14.97, 14.38, 6.5, 4.33, 52.2, 39.17, 30.97, 75, 62.5, 38.33, 87.5, 62.5, 57.5, 45.02, 31.02, 26.07, 46.72, 34.32, 21.5, 50.83, 34.23, 25.25, 45.83, 33.47, 27.7, 67.67, 57.5, 52.67, 30.98, 23.62, 9.1, 18.17, 18.57, 10.15, 46.67, 34.27, 23.62, 54.17, 40.05, 29.37, 70.83, 59.17, 47.53, 8.67, 5.63, 0.87, 9.87, 3.03, 0, 17.75, 6.88, 0, 62.5, 37.5, 27.7, 70.83, 57.5, 50.83, 6.5, 2.17, 1.3, 6.93, 3.03, 0.53, 14.82, 5.2, 0, 37.5, 28.52, 13, 75, 37.5, 37.5, 15.3, 9.53, 5.63, 9.43, 3.03, 0.43, 16.4, 6.07, 0, 57.5, 34.23, 21.98, 78.33, 62.5, 37.5, 12.08, 6.5, 1.3, 10.73, 3.03, 0, 15.2, 3.9, 0.43, 62.5, 37.5, 21.98, 64.17, 55.83, 41.73, 8.73, 3.57, 0, 8.57, 2.17, 0, 16.5, 7.7, 0.43, 42.58, 36.68, 13, 65.83, 47.5, 37.5, 8.03, 5.07, 0.43, 10.68, 7.27, 3.5, 48.38, 38.42, 24.83, 45.03, 38.4, 30.8, 73.33, 63.33, 50.83, 3.37, 2.17, 0.9, 9, 6.02, 5.2, 21.07, 12.37, 6.02, 45.02, 32.65, 21.67, 68.78, 56.68, 50, 0, 0, 0, 7.8, 4.33, 4.33, 25.17, 20.65, 13.15, 48.37, 39.23, 27.17, 75.83, 62.5, 49, 11.78, 12.72, 3.8, 20.18, 14.87, 8.95, 46.7, 39.32, 33.03, 49.18, 40.05, 24.43, 69.17, 60, 48.33, 0, 0, 0, 15.25, 9.82, 7.75, 45.9, 38.47, 35.52, 50.88, 37.61, 33.47, 79.17, 71.67, 58.33)), .Names = c("Site", "Field", "Leaf_Location", "Time", "Damage"), row.names = c(NA, -225L), class = "data.frame")
Here's a simplified reproducible example to explain first, some dummy data: per = data.frame(x=rep(c('a','b'), each=100), y=c(2+rnorm(100), 3+rnorm(100,0,2))) Now you are plotting the error bars, using fun.ymin=min, fun.ymax=max, which will cause them to extend the full range of the data, as in the following graph: ggplot(per, aes(x, y)) + stat_summary(fun.y = mean, geom="bar") + geom_point(position = position_jitter(0.1)) + stat_summary(fun.ymin=min, fun.ymax=max, geom="errorbar", width=0.4) + theme_bw() Whereas, it is more conventional to use error bars that extend either +/- one standard deviation, as in the following: ggplot(per, aes(x, y)) + stat_summary(fun.y = mean, geom="bar") + stat_summary( fun.ymin=function(y) {mean(y) - sd(y)}, fun.ymax=function(y) {mean(y) + sd(y)}, geom="errorbar", width=0.2) + theme_bw() Or one standard error, like this: ggplot(per, aes(x, y)) + stat_summary(fun.y = mean, geom="bar") + stat_summary( fun.ymin=function(y) {mean(y) - sqrt(var(y)/length(y))}, fun.ymax=function(y) {mean(y) + sqrt(var(y)/length(y))}, geom="errorbar", width=0.2) + theme_bw() EDIT - example data were added to question, after this answer was originally posted We can applying exactly the same approach as above to your example data: ggplot(per, aes(x=Leaf_Location, y=Damage, fill=as.factor(Time))) + stat_summary(fun.y=mean, geom="bar",position=position_dodge(),colour="black",width=.7,size=.7) + stat_summary( fun.ymin=function(y) {mean(y) - sqrt(var(y)/length(y))}, fun.ymax=function(y) {mean(y) + sqrt(var(y)/length(y))}, geom="errorbar", position=position_dodge(.7), width=.2)
How to draw polygon/ convex hull around Partitioned Around Medoids (PAM) clusters in R?
Good Day Is it possible to produce a plot based on the output of a PAM dissimilarity clustering analysis with polygons drawn around the outer point of the clusters? I have currently achieved something similar using the function clusplot however am more interested in seeing the clusters demarcated using straight lines. # Installing packages library(cluster) library(fpc) library(ggplot2) library(ggfortify) #Importing Koeberg matrix into R KoebergAllCSV <- read.csv("C:/R/Koeberg Cluster/KoebergAllCSV.csv", row.names=1, sep=";") #Checking if data is in the correct format/Checking class/mode of each column sapply(KoebergAllCSV, mode) sapply(KoebergAllCSV, class) #Creating gower dissimilarity matrix using function "daisy" #specifying variable type(numerics all ratioscaled and log transformed) #and weighting all columns as 1 Koeberg.Diss = daisy(KoebergAllCSV, metric = "gower", type = list(logratio = c("Mass", "EF")), weights = rep.int(1,5)) attributes(Koeberg.Diss) #Determine k pamk(Koeberg.Diss, krange=2:50, criterion="asw", usepam=TRUE, scaling=FALSE, diss=TRUE, critout=FALSE) #Run cluster analysis using PAM (Partitioning around medoids) pam_fit= pam(Koeberg.Diss, diss = TRUE, k = 28) #Export cluster info KoebergClusInfo = paste("KoebergClusInfo", ".txt") write.table(pam_fit$clustering, file = KoebergClusInfo, sep=",") ## Default S3 method: clusplot(Koeberg.Diss, pam_fit$clustering, diss = TRUE, stand = FALSE, lines = 0, labels= 4, xlim = c(-1,1), plotchar = TRUE, span = TRUE, shade = TRUE, color = TRUE, col.p = "black", main = "Koeberg gower/pam Clusterplot", verbose = getOption("verbose")) I am aware that the function autoplot in ggplot2 accepts objects of class pam however when attempting to use it for my data and replacing the above clusplot function with autoplot(pam(pam_fit), frame = TRUE) or autoplot(pam(Koeberg.Diss, diss = TRUE, k = 28), frame = TRUE) I get the following errors... Error in pam(pam_fit) : x is not a numeric dataframe or matrix. and Error in as.data.frame.default(x[[i]], optional = TRUE, stringsAsFactors = stringsAsFactors) : cannot coerce class ""waiver"" to a data.frame Respectively... I am relatively new to R and posting questions in these forums, so any help would be massively appreciated. Edit: Got it to work using the fviz_cluster() in the factoextra package # Installing packages library(cluster) library(fpc) library(factoextra) #Importing Koeberg matrix into R KoebergAllCSV <- read.csv("C:/R/Koeberg Cluster/KoebergAllCSV.csv", row.names=1, sep=";") #creating gower dissimilarity matrix using daisy Koeberg.Gower = as.matrix(daisy(KoebergAllCSV, metric = "gower", type = list(logratio = c("Mass", "EF")))) attributes(Koeberg.Gower) pamk(Koeberg.Gower, krange=2:50, criterion="asw", usepam=TRUE, scaling=FALSE, diss=TRUE, critout=FALSE) Koeberg.Pam = pam(Koeberg.Gower, 28, diss = TRUE, keep.diss = TRUE) fviz_cluster(object = list(data=Koeberg.Gower, cluster = Koeberg.Pam$clustering), geom = c("point", "text"), ellipse.type = "convex", stand = FALSE) fviz_silhouette(silhouette(Koeberg.Pam))
# Installing packages library(cluster) library(fpc) library(factoextra) #Importing Koeberg matrix into R KoebergAllCSV <- read.csv("C:/R/Koeberg Cluster/KoebergAllCSV.csv", row.names=1, sep=";") #creating gower dissimilarity matrix using daisy Koeberg.Gower = as.matrix(daisy(KoebergAllCSV, metric = "gower", type = list(logratio = c("Mass", "EF")))) attributes(Koeberg.Gower) pamk(Koeberg.Gower, krange=2:50, criterion="asw", usepam=TRUE, scaling=FALSE, diss=TRUE, critout=FALSE) Koeberg.Pam = pam(Koeberg.Gower, 28, diss = TRUE, keep.diss = TRUE) fviz_cluster(object = list(data=Koeberg.Gower, cluster = Koeberg.Pam$clustering), geom = c("point", "text"), ellipse.type = "convex", stand = FALSE) fviz_silhouette(silhouette(Koeberg.Pam)) Data used: "KoebergAllCSV" structure(list(Mass = c(157000, 775, 197, 15000, 3250, 628, 1815, 2070, 2000, 1218, 614, 536, 379, 235, 800, 672, 1960, 768, 1540, 1790, 3500, 7450, 4030, 2200, 830, 1180, 1310, 955, 590, 1168, 820, 790, 5000, 883, 824, 280, 184, 941, 293, 1250, 3900, 1700, 925, 220, 1040, 510, 690, 600, 539, 1018, 122, 1086, 118, 737, 370, 1236, 5820, 229, 226, 220, 305.5, 94.5, 390, 198, 445, 623, 1100, 377, 340, 418, 326, 202, 139, 47, 35.1, 46.1, 580, 1150, 66, 44, 50, 30, 34.2, 30, 91, 71, 59, 78.9, 110, 405, 19.5, 73, 64, 39, 54, 39, 37, 48, 21.2, 26.3, 24.2, 29, 15.2, 35, 16.1, 16.8, 29.7, 12.5, 55, 612, 630, 865, 22.4, 8.6, 47.3, 32.5, 28.8, 17.3, 38, 23.5, 22, 15.5, 18.1, 34, 23, 13.1, 13, 14.7, 19.1, 14, 18.6, 15.5, 37, 14.5, 24.6, 25, 28.5, 50.8, 52, 68.8, 76.1, 100, 85, 158, 113, 88, 25.6, 13, 10.2, 30.5, 38, 55, 45.5, 30, 52, 11, 17.8, 29, 13, 23.2, 38, 21, 25, 27.3, 427, 1572, 78.9, 15, 61, 212.9, 700, 11.1, 44, 29.6, 124, 3200, 5800, 5300, 950, 62.4, 205, 270, 93, 40.2, 102, 240, 90, 33, 16.6, 39.2, 47, 60.8, 13, 20.8, 8, 11, 165000, 180000, 63600, 11400, 21200, 41000, 11300, 840000, 240000, 320000, 900, 4090, 1250, 19000, 19000, 6400, 2610, 47, 4500, 1258, 238, 55, 113, 9990, 5360, 17800, 110.1973216, 238.1629085, 89.33169378, 245.0708356, 83.49190575, 7.323754897, 17.91558243, 2.259871723, 1.992123644, 78.63046291, 235.6804221, 413.5582987, 486.5966599, 7.418054089, 8.4510848, 8.4510848, 42.83324573, 8.4510848, 3.14445177, 2000, 496.2334891, 119.4158615, 805.4349144, 8.212468482, 25.0905618), Diet = structure(c(4L, 2L, 2L, 6L, 2L, 2L, 2L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 1L, 1L, 1L,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620513, 1325.913225, 831.6334703, 758.1419376, 598.7459669, 432.4008244, 995.8492032, 1104.982804, 1833.224631, 968.5460848, 1555.574839, 2526.177199, 2720.81891, 4551.218864, 2995.035921, 1983.25768, 1021.131045, 1297.600326, 1393.320578, 1123.496167, 809.3558665, 1288.599252, 1012.736663, 987.3550419, 3468.868783, 1065.095472, 1016.098313, 487.1962293, 366.0414243, 1112.253632, 502.488525, 1349.53769, 2928.89833, 1663.891544, 1099.339465, 413.4082553, 1190.663398, 732.8997761, 900.420823, 818.6726853, 354.4240516, 652.168291, 85.2606497, 693.8895747, 270.483605, 941.7478954, 589.026282, 1339.226017, 3846.819879, 533.5361702, 528.1460593, 517.3161179, 666.1149499, 269.8814521, 803.9152978, 477.0047921, 889.8724349, 1153.045225, 1786.335023, 783.201274, 723.3180648, 640.0447608, 540.3690726, 390.0619447, 302.4004117, 144.5066856, 118.4522857, 142.6164524, 799.9885345, 1275.042111, 182.0952927, 543.7188737, 634.8358004, 341.8068213, 400.635478, 341.8068213, 1311.800923, 191.3798609, 168.7094925, 205.6358063, 257.8562828, 626.4208001, 79.37849663, 195.0348258, 178.3190991, 127.263627, 158.8361858, 127.263627, 122.7819915, 144.6363257, 82.97394225, 96.07341942, 90.78786186, 102.6748266, 66.17393783, 116.6808568, 68.813704, 70.83430724, 104.3536694, 57.93381766, 158.6645733, 816.6280747, 832.8847551, 1033.247332, 86.13942963, 44.9254345, 143.1986471, 110.9466069, 102.1927841, 72.26112014, 123.3917674, 88.99374555, 85.0904386, 67.05928121, 74.51689843, 114.4034188, 87.7017517, 59.81055334, 59.49970587, 64.68582581, 77.29226408, 62.57493448, 75.91055058, 67.05928121, 121.1742978, 64.08606129, 91.80560821, 92.81807245, 101.4677065, 150.3213487, 152.7269268, 184.7538415, 197.8677001, 238.2502359, 213.3232585, 325.1772534, 258.896799, 218.4145451, 94.32710718, 59.49970587, 50.45235653, 106.2569217, 123.3917674, 158.6645733, 139.4700925, 105.0692888, 152.7269268, 53.11049696, 73.67479581, 102.6748266, 59.49970587, 88.21961721, 123.3917674, 82.44084978, 92.81807245, 98.54258242, 649.397604, 1577.514936, 202.7896107, 65.58060147, 170.2384385, 404.275036, 909.2871722, 53.43834074, 136.3267745, 104.1146163, 265.4017335, 2559.743414, 3837.812585, 3609.284914, 1119.48705, 196.0571475, 393.9976926, 605.6763891, 266.5768403, 139.7476799, 286.2283703, 438.6449711, 224.9201933, 112.1044413, 70.25978867, 126.0282381, 142.5804177, 169.8586911, 59.49970587, 81.90613014, 42.76953519, 53.11049696, 44893.11086, 48012.29543, 21505.57155, 5704.435068, 9209.019243, 15323.26221, 5665.766265, 157697.8254, 59952.20689, 74861.38869, 616.5285774, 2297.756619, 820.2217331, 23289.68486, 8728.776034, 3390.680499, 1555.167143, 82.25108625, 2783.313695, 2329.752262, 567.6985933, 163.9110073, 301.8499294, 4992.739194, 2906.435392, 8247.673366, 12.81581191, 25.42711978, 10.63408241, 26.08172622, 10.0137771, 1.178076499, 2.549050089, 0.353356528, 0.31350088, 9.49371787, 25.19136319, 41.52955076, 47.98985328, 1.091673606, 1.235456699, 1.235456699, 5.76431571, 1.235456699, 0.483456886, 112.0018952, 48.83385255, 13.76461928, 75.11335195, 1.274157763, 3.438909954)), .Names = c("Mass", "Diet", "Time", "Space", "EF"), class = "data.frame", row.names = c("CommonOstrich", "GreatCrestedGrebe", "LittleGrebe", "GreatWhitePelican", "White-breastedCormorant", "ReedCormorant", "AfricanDarter", "GreyHeron", "Black-headedHeron", "PurpleHeron", "LittleEgret", "Yellow-billedEgret", "CattleEgret", "Green-backedHeron", "Black-crownedNight-Heron", "HamerkopHamerkop", "AfricanSacredIbis", "GlossyIbis", "HadedaIbis", "AfricanSpoonbill", "GreaterFlamingo", "Spur-wingedGoose", "EgyptianGoose", "SouthAfricanShelduck", "CapeShoveler", "AfricanBlackDuck", "Yellow-billedDuck", "Red-billedTeal", "CapeTeal", "SouthernPochard", "MaccoaDuck", "White-backedDuck", "Secretarybird", "PeregrineFalcon", "LannerFalcon", "RockKestrel", "LesserKestrel", "Yellow-billedKite", "Black-shoulderedKite", "BootedEagle", "AfricanFish-eagle", "JackalBuzzard", "SteppeBuzzard", "Rufous-chestedSparrowhawk", "BlackSparrowhawk", "AfricanGoshawk", "AfricanMarsh-harrier", "BlackHarrier", "Grey-wingedFrancolin", "CapeSpurfowl", "CommonQuail", "HelmetedGuineafowl", "BlackCrake", "AfricanPurpleSwamphen", "CommonMoorhen", "Red-knobbedCoot", "BlueCrane", "CrownedLapwing", "BlacksmithLapwing", "RuffRuff", "CommonGreenshank", "WoodSandpiper", "PiedAvocet", "Black-wingedStilt", "WaterThick-knee", "SpottedThick-knee", "KelpGull", "Grey-headedGull", "Hartlaub'sGull", "SpeckledPigeon", "Red-eyedDove", "CapeTurtle-dove", "LaughingDove", "NamaquaDove", "Klaas'sCuckoo", "DiderickCuckoo", "BarnOwl", "SpottedEagle-owl", "Fiery-neckedNightjar", "CommonSwift", "AfricanBlackSwift", "White-rumpedSwift", "HorusSwift", "LittleSwift", "AlpineSwift", "SpeckledMousebird", "White-backedMousebird", "Red-facedMousebird", "PiedKingfisher", "GiantKingfisher", "MalachiteKingfisher", "EuropeanBee-eater", "AfricanHoopoe", "AcaciaPiedBarbet", "GreaterHoneyguide", "LesserHoneyguide", "CardinalWoodpecker", "Large-billedLark", "Grey-backedSparrowlark", "Red-cappedLark", "BarnSwallow", "White-throatedSwallow", "Pearl-breastedSwallow", "GreaterStripedSwallow", "RockMartin", "Brown-throatedMartin", "BandedMartin", "Black(Southernrace)Saw-wing", "Fork-tailedDrongo", "PiedCrow", "CapeCrow", "White-neckedRaven", "GreyTit", "CapePenduline-tit", "CapeBulbul", "CappedWheatear", "FamiliarChat", "AfricanStonechat", "CapeRobin-chat", "KarooScrub-robin", "LesserSwamp-warbler", "AfricanReed-warbler", "LittleRush-warbler", "CapeGrassbird", "Long-billedCrombec", "Bar-throatedApalis", "CloudCisticola", "Grey-backedCisticola", "Levaillant'sCisticola", "AfricanDuskyFlycatcher", "Chestnut-ventedTit-babbler", "Layard'sTit-babbler", "FiscalFlycatcher", "CapeBatis", "AfricanParadise-flycatcher", "CapeWagtail", "AfricanPipit", "CapeLongclaw", "Common(Southern)Fiscal", "SouthernBoubou", "Bokmakierie", "CommonStarling", "WattledStarling", "Red-wingedStarling", "PiedStarling", "CapeSugarbird", "MalachiteSunbird", "Orange-breastedSunbird", "SouthernDouble-collaredSunbird", "HouseSparrow", "CapeSparrow", "CapeWeaver", "SouthernMasked-weaver", "SouthernRedBishop", "YellowBishop", "CommonWaxbill", "Pin-tailedWhydah", "CapeCanary", "Black-headedCanary", "BrimstoneCanary", "White-throatedCanary", "YellowCanary", "Streaky-headedSeedeater", "CapeBunting", "RockDove", "MallardDuck", "OliveThrush", "CapeWhite-eye", "CapeLong-billedLark", "Burchell'sCoucal", "SouthernBlackKorhaan", "KarooPrinia", "CapeClapperLark", "SouthernGrey-headedSparrow", "LittleBittern", "BlackStork", "Verreaux'sEagle", "MartialEagle", "AfricanHarrier-Hawk", "BlackrumpedButtonquail", "AfricanRail", "AfricanJacana", "CommonSandpiper", "LittleStint", "White-wingedTern", "NamaquaSandgrouse", "Red-chestedCuckoo", "KarooLark", "SandMartin", "SombreGreenbul", "MountainChat", "Ant-eatingChat", "ZittingCisticola", "SpottedFlycatcher", "FairyFlycatcher", "DuskySunbird", "RedHartebeest", "BlueWildebeest", "Bontebok", "CapeGrysbok", "CommonDuiker", "Springbok", "Steenbok", "CommonEland", "Gemsbok", "PlainsZebra", "YellowMongoose", "LargeGreyMongoose", "SmallGreyMongoose", "CapePorcupine", "Caracal", "AfricanWildCat", "Small-spottedGenet", "CapeGoldenMole", "ScrubHare", "CapeDuneMole-Rat", "VleiRat", "FourStripedGrassMouse", "CapeGerbil", "Black-BackedJackal", "Bat-earedFox", "AfricanClawlessOtter", "HeraldSnake", "RhombicEgg-eater", "SpottedHarlequinSnake", "OliveHouseSnake", "SpottedHouseSnake", "Knox'sDesertLizard", "NamaquaDwarfChameleon", "Austen'sThick-toedGecko", "OcelatedThick-toedGecko", "CrossedWhipSnake", "CapeWhipSnake", "Spotted/RhombicSkaapsteker", "MoleSnake", "Short-leggedseps", "SilveryDwarfBurrowingSkink", "BloubergDwarfBurrowingSkink", "CapeSkink", "Red-SidedSkink", "VariegatedSkink", "AngulateTortoise", "Boomslang", "KarooWhipSnake", "CapeCobra", "Delalande'sBeakedBlindSnake", "CapeGirdledLizard"))