How to set placeholders in R - 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))

Related

How to get right proportions based on a subset of the data set in R

I want to calculate the proportion of plan with respect to school type in the dataset below. Thing is, I have to first subset the dataset a couple of times. In this case, for example, I need to subset the dataset so that I only get schools which offer from level 0 up to level 04. How can I do that?
edit:
The way below is working, but I'm not filtering all schools that have all levels from level 0 up to level 04 , I'm getting the schools that have either of them. Ideas to that would be much appreciated.
data description:
# SCHOOL = name of the school
# Q9 = type of school
# Q11 = levels that the schools offer
# Q40 = types of planning that the school offers related to each level.
Note: All plannings are related to a school level. Hence, If I don't filter by the levels that each school offers and just calculate this columns' prop, my results will be misleading because each school can offer different level options (all of them, 1, 2 of them, etc)
my attempt:
### D) filter schools which only offer UP TO Level 04 ###
##################################### Quest40_2 ##########################
### check vector's names:
unique(quest40_2$Q11)
unwanted_4_2 <- quest40_2 %>%
filter(Q11 %in% c('level05','level06')) ### FILTER UNWANTED SCHOOL LEVELS
### create a vector with UNWANTED SCHOOL NAMES:
filter_vec_4_2 <- unique(unwanted_4_2$SCHOOL) ### get unique names (only school has 1 name)
### assign the original dataframe to a dummy data frame:
filtered_df_4_2 <- quest40_2
### loop over unwanted schools' names to remove them:
for (i in 1 :length(filter_vec_4_2)) {
filtered_df_4_2 <- filtered_df_4_2[!filtered_df_4_2$SCHOOL == filter_vec_4_2[i],]
}
I need to count how many times EACH 'plan' occurs by each type of 'school' (and get its proportion). Problem = each school can have more than one 'plan' type. Since each school can have more than one plan type, if we want to count the proportion of
plan types according to each school type, we cannot divide by n
b <- filtered_df_4_2 %>% drop_na(Q40) %>%
count(Q40, Q9) %>%
group_by(Q9)
Which leaves me to:
### counting the unique schools by each type of 'curriculo':
b2 <- b2 %>%
select(SCHOOL, Q9) %>%
unique() %>% count(Q9)
## join dataframes and get the proportion of schools that have each type of 'planejamento'
## within the curriculum types
c <- b %>% full_join(b2, by = 'Q9') %>%
mutate(prop = round((n.x/n.y *100), digits = 2)) %>%
select(-n.x, -n.y)
Q1 = I don't think I'm filtering it correctely since I'm not exclusively filtering the schools that offer alll levels up to 01 to 04. I guess I'm doing an 'or' not an 'and'
Q2 = Is there a way to avoid the loop with tidyverse ? Thanks in adv.
Ultimately, I'm trying to get the starts to => 'do the schools plan their agenda for the levels they offer or not and how does the type of school impact this?' (later this can be modeled, but now I just need percentages)
data:
dput(quest40_2)
structure(list(SCHOOL = structure(c("School1", "School1", "School1",
"School1", "School1", "School1", "School1", "School2", "School2",
"School2", "School2", "School2", "School2", "School2", "School3",
"School3", "School3", "School3", "School3", "School3", "School3",
"School3", "School3", "School4", "School4", "School4", "School4",
"School4", "School4", "School5", "School5", "School5", "School5",
"School5", "School5", "School6", "School6", "School6", "School6",
"School6", "School7", "School7", "School7", "School7", "School7",
"School7", "School8", "School8", "School8", "School8", "School9",
"School9", "School9", "School9", "School9", "School10", "School10",
"School10", "School10", "School10", "School11", "School11", "School11",
"School11", "School11", "School11", "School11", "School11", "School11",
"School12", "School12", "School12", "School12", "School12", "School12",
"School12", "School13", "School13", "School13", "School13", "School13",
"School13", "School13", "School13", "School13", "School13", "School14",
"School14", "School14", "School15", "School15", "School15", "School15",
"School15", "School15", "School16", "School16", "School16", "School16",
"School16", "School16", "School16", "School17", "School17", "School17",
"School17", "School17", "School18", "School18", "School18", "School18",
"School18", "School19", "School19", "School19", "School19", "School19",
"School19", "School20", "School20", "School20", "School21", "School21",
"School21", "School21", "School21", "School22", "School22", "School22",
"School22", "School22", "School23", "School23", "School23", "School23",
"School23", "School23", "School24", "School24", "School24", "School24",
"School24", "School24", "School25", "School25", "School25", "School25",
"School25", "School26", "School26", "School26", "School26", "School26",
"School26", "School26", "School26", "School26", "School27", "School27",
"School27", "School27", "School27", "School27", "School27", "School28",
"School28", "School28", "School28", "School28", "School28", "School28",
"School28", "School29", "School29", "School29", "School29", "School29",
"School29", "School30", "School30", "School30", "School30", "School30",
"School30", "School30", "School30", "School30", "School30", "School30",
"School31", "School31", "School31", "School31", "School31", "School31",
"School31", "School31", "School31", "School31", "School31", "School32",
"School32", "School32", "School32", "School32", "School32", "School32",
"School32", "School32", "School32", "School32", "School33", "School33",
"School33", "School33", "School33", "School33", "School33", "School34",
"School34", "School34", "School34", "School34", "School34", "School34",
"School34", "School35", "School35", "School35", "School35", "School36",
"School36", "School36", "School36", "School36", "School37", "School37",
"School37", "School37", "School37", "School37", "School37", "School37",
"School38", "School38", "School38", "School38", "School39", "School39",
"School39", "School39", "School39", "School39"), class = c("glue",
"character")), Q9 = 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, 3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L,
3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L, 4L,
4L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 3L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L, 3L, 4L, 4L, 4L, 4L, 4L, 3L, 3L, 3L, 3L,
3L, 3L, 3L, 3L, 3L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L,
2L, 2L, 2L, 2L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L, 4L, 2L, 1L,
5L, 6L, NA, NA, NA, NA, NA, 7L, 4L, 2L, 1L, 5L, 6L, NA, NA, NA,
NA, NA, 7L, 4L, 2L, 1L, 5L, 6L, NA, NA, NA, NA, NA, 7L, 4L, 2L,
1L, 5L, 6L, NA, 7L, 4L, 2L, 1L, 5L, NA, NA, NA, 3L, 7L, 4L, NA,
7L, 4L, 2L, NA, NA, 4L, 2L, 1L, 5L, NA, NA, NA, NA, 4L, 2L, 1L,
NA, 7L, 4L, 2L, 1L, 5L, NA), .Label = c("level04", "level03",
"level0", "level02", "level05", "level06", "level01"), class = "factor"),
Q40 = structure(c(NA, NA, NA, NA, 2L, 6L, 5L, NA, NA, NA,
NA, 2L, 6L, 5L, NA, NA, NA, NA, NA, 2L, 6L, 5L, 4L, NA, NA,
NA, 2L, 6L, 5L, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, 1L,
NA, NA, NA, NA, NA, 1L, NA, NA, NA, 1L, NA, NA, NA, NA, 1L,
NA, NA, NA, NA, 1L, NA, NA, NA, NA, 2L, 6L, 5L, 4L, 1L, NA,
NA, NA, NA, 2L, 6L, 5L, NA, NA, NA, NA, NA, NA, 2L, 6L, 4L,
3L, NA, NA, 1L, NA, NA, NA, NA, NA, 1L, NA, NA, NA, NA, 2L,
6L, 5L, NA, NA, NA, NA, 2L, NA, NA, NA, NA, 1L, NA, NA, NA,
2L, 6L, 5L, NA, NA, 1L, NA, NA, NA, NA, 1L, NA, NA, NA, NA,
1L, NA, NA, NA, NA, 2L, 1L, NA, NA, NA, 2L, 6L, 5L, NA, NA,
NA, NA, 1L, NA, NA, NA, NA, NA, 2L, 6L, 5L, 4L, NA, NA, NA,
2L, 6L, 5L, 4L, NA, NA, NA, NA, NA, NA, NA, 2L, NA, NA, NA,
NA, 2L, 1L, NA, NA, NA, NA, NA, NA, 2L, 6L, 5L, 4L, 3L, NA,
NA, NA, NA, NA, NA, 2L, 6L, 5L, 4L, 3L, NA, NA, NA, NA, NA,
NA, 2L, 6L, 5L, 4L, 3L, NA, NA, NA, NA, NA, NA, 1L, NA, NA,
NA, NA, NA, 2L, 6L, 5L, NA, NA, NA, 2L, NA, NA, NA, 2L, 6L,
NA, NA, NA, NA, 2L, 6L, 5L, 4L, NA, NA, NA, 1L, NA, NA, NA,
NA, NA, 1L), .Label = c("none", "plan1_level0upto02", "plan5_level_05",
"plan4_level_05", "plan3_level_04", "plan2_level_03"), class = "factor")), row.names = c(NA,
-253L), class = c("tbl_df", "tbl", "data.frame"))
This might be what you need or give you a good start. It shows the number of plans per type with levels 0-4, also shows the percentages.
left_join(na.omit(quest40_2[, c("SCHOOL", "Q9", "Q11")]),
na.omit(quest40_2[, c("SCHOOL", "Q40")]), c("SCHOOL"),
multiple = "all") %>%
group_by(Q9, Q40) %>%
filter(sub(".*(\\d+)$", "\\1", Q11) <= 4) %>%
summarize(n = n(), .groups = "drop") %>%
mutate(percentage = n / sum(n) * 100) %>%
print(n = Inf)
# A tibble: 23 × 4
Q9 Q40 n percentage
<fct> <fct> <int> <dbl>
1 typeA none 3 0.974
2 typeA plan1_level0upto02 6 1.95
3 typeA plan5_level_05 4 1.30
4 typeA plan4_level_05 6 1.95
5 typeA plan3_level_04 6 1.95
6 typeA plan2_level_03 6 1.95
7 typeB none 21 6.82
8 typeB plan1_level0upto02 47 15.3
9 typeB plan5_level_05 4 1.30
10 typeB plan4_level_05 14 4.55
11 typeB plan3_level_04 31 10.1
12 typeB plan2_level_03 31 10.1
13 typeC none 32 10.4
14 typeC plan1_level0upto02 21 6.82
15 typeC plan4_level_05 4 1.30
16 typeC plan3_level_04 15 4.87
17 typeC plan2_level_03 18 5.84
18 typeD none 3 0.974
19 typeD plan1_level0upto02 8 2.60
20 typeD plan5_level_05 8 2.60
21 typeD plan4_level_05 8 2.60
22 typeD plan3_level_04 4 1.30
23 typeD plan2_level_03 8 2.60

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,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, 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, 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, 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, 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")

Setting names of new calculated variables and assign values using a function

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(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, 4L,
NA, 4L, NA, 3L, 3L, 2L, 4L, 3L, 3L, 3L, 4L, 5L, 4L, NA, NA, 3L,
3L, 3L, 5L, 3L, 3L, 4L, NA, 4L, 4L, 3L, 3L, 4L, 3L, 4L, 3L, 4L,
4L, NA, 4L, 3L, 4L, 3L, 4L, 4L, NA, 5L, 4L, 3L, 4L, 3L, 4L, 3L,
NA, NA, 3L, NA, 3L, 4L, 3L, 3L, NA, 4L, 4L, 4L, 4L, 3L, 4L, 3L,
2L, 2L, 3L, 4L, 4L, 2L, 2L, 4L, 2L, 2L, 4L, 2L, 2L, 4L, 3L, 2L,
3L, 4L, 4L, 3L, 4L,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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, N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, 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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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)

Tranpose and Calculate pearson correlation

I am really new to coding and I need to run a number of statistics in a dataset, for example the pearson correlation, but I am having some trouble manipulating the data.
From what I understood I need to transpose my data in order to calculate the pearson correlation, but here's where I'm having some problems. For starters, the column names turn into a new row instead of becoming the new column names. Then I get a message that my values are not numeric.
I also have some NA and I am trying to calculate the correlation with this code
cor(cr, use = "complete.obs", method = "pearson")
Error in cor(cr1, use = "complete.obs", method = "pearson") :
'x' must be numeric
I need to know the correlation between Victoria and Nuria which should yield 0.3651484
here is the dput of my dataset:
> dput(cr)
structure(list(User = structure(c(8L, 10L, 2L, 17L, 11L, 1L,
18L, 9L, 7L, 5L, 3L, 14L, 13L, 4L, 20L, 6L, 16L, 12L, 15L, 19L
), .Label = c("Ana", "Anton", "Bernard", "Carles", "Chris", "Ivan",
"Jim", "John", "Marc", "Maria", "Martina", "Nadia", "Nerea",
"Nuria", "Oriol", "Rachel", "Roger", "Sergi", "Valery", "Victoria"
), class = "factor"), Star.Wars.IV...A.New.Hope = c(1L, 5L, NA,
NA, 4L, 2L, NA, 4L, 5L, 4L, 2L, 3L, 2L, 3L, 4L, NA, NA, 4L, 5L,
1L), Star.Wars.VI...Return.of.the.Jedi = c(5L, 3L, NA, 3L, 3L,
4L, NA, NA, 1L, 2L, 1L, 5L, 3L, NA, 4L, NA, NA, 5L, 1L, 2L),
Forrest.Gump = c(2L, NA, NA, NA, 4L, 4L, 3L, NA, NA, NA,
5L, 2L, NA, 3L, NA, 1L, NA, 1L, NA, 2L), The.Shawshank.Redemption = c(NA,
2L, 5L, NA, 1L, 4L, 1L, NA, 4L, 5L, NA, NA, 5L, NA, NA, NA,
NA, 5L, NA, 4L), The.Silence.of.the.Lambs = c(4L, 4L, 2L,
NA, 4L, NA, 1L, 3L, 2L, 3L, NA, 2L, 4L, 2L, 5L, 3L, 4L, 1L,
NA, 5L), Gladiator = c(4L, 2L, NA, 1L, 1L, NA, 4L, 2L, 4L,
NA, 5L, NA, NA, NA, 5L, 2L, NA, 1L, 4L, NA), Toy.Story = c(2L,
1L, 4L, 2L, NA, 3L, NA, 2L, 4L, 4L, 5L, 2L, 4L, 3L, 2L, NA,
2L, 4L, 2L, 2L), Saving.Private.Ryan = c(2L, NA, NA, 3L,
4L, 1L, 5L, NA, 4L, 3L, NA, NA, 5L, NA, NA, 2L, NA, NA, 1L,
3L), Pulp.Fiction = c(NA, NA, NA, 4L, NA, 4L, 2L, 3L, NA,
4L, NA, 1L, NA, NA, 3L, NA, 2L, 5L, 3L, 2L), Stand.by.Me = c(3L,
4L, 1L, NA, 1L, 4L, NA, NA, 1L, NA, NA, NA, NA, 4L, 5L, 1L,
NA, NA, 3L, 2L), Shakespeare.in.Love = c(2L, 3L, NA, NA,
5L, 5L, 1L, NA, 2L, NA, NA, 3L, NA, NA, NA, 5L, 2L, NA, 3L,
1L), Total.Recall = c(NA, 2L, 1L, 4L, 1L, 2L, NA, 2L, 3L,
NA, 3L, NA, 2L, 1L, 1L, NA, NA, NA, 1L, NA), Independence.Day = c(5L,
2L, 4L, 1L, NA, 4L, NA, 3L, 1L, 2L, 2L, 3L, 4L, 2L, 3L, NA,
NA, NA, NA, NA), Blade.Runner = c(2L, NA, 4L, 3L, 4L, NA,
3L, 2L, NA, NA, NA, NA, NA, 2L, NA, NA, NA, 4L, NA, 5L),
Groundhog.Day = c(NA, 2L, 1L, 5L, NA, 1L, NA, 4L, 5L, NA,
NA, 2L, 3L, 3L, 2L, 5L, NA, NA, NA, 5L), The.Matrix = c(4L,
NA, 1L, NA, 3L, NA, 1L, NA, NA, 2L, 1L, 5L, NA, 5L, NA, 2L,
4L, NA, 2L, 4L), Schindler.s.List = c(2L, 5L, 2L, 5L, 5L,
NA, NA, 1L, NA, 5L, NA, NA, NA, 1L, 3L, 2L, NA, 2L, NA, 3L
), The.Sixth.Sense = c(5L, 1L, 3L, 1L, 5L, 3L, NA, 3L, NA,
1L, 2L, NA, NA, NA, NA, 4L, NA, 1L, NA, 5L), Raiders.of.the.Lost.Ark = c(NA,
3L, 1L, 1L, NA, NA, 5L, 5L, NA, NA, 1L, NA, 5L, NA, 3L, 3L,
NA, 2L, NA, 3L), Babe = c(NA, NA, 3L, 2L, NA, 2L, 2L, NA,
5L, NA, 4L, 2L, NA, NA, 1L, 4L, NA, 5L, NA, NA)), .Names = c("User",
"Star.Wars.IV...A.New.Hope", "Star.Wars.VI...Return.of.the.Jedi",
"Forrest.Gump", "The.Shawshank.Redemption", "The.Silence.of.the.Lambs",
"Gladiator", "Toy.Story", "Saving.Private.Ryan", "Pulp.Fiction",
"Stand.by.Me", "Shakespeare.in.Love", "Total.Recall", "Independence.Day",
"Blade.Runner", "Groundhog.Day", "The.Matrix", "Schindler.s.List",
"The.Sixth.Sense", "Raiders.of.the.Lost.Ark", "Babe"), row.names = c(NA,
-20L), class = c("tbl_df", "tbl", "data.frame"))
Can someone help me?
This code should give you the correlation matrix between all users.
cr2<-t(cr[,2:21]) # Transpose (first column contains names)
colnames(cr2)<-cr[,1] # Assign names to columns
cor(cr2,use="complete.obs") # Gives an error because there are no complete obs
# Error in cor(cr2, use = "complete.obs") : no complete element pairs
cor(cr2,use="pairwise.complete.obs") # use pairwise deletion
Correlation between Victoria and Nuria is 0.36514837 (using pairwise deletion)
Edit:To get just the correlation between Victoria and Nuria with listwise deletion, run the above and then
cr2<-as.data.frame(cr2)
with(cr2, cor(Victoria, Nuria, use = "complete.obs", method = "pearson"))
[1] 0.3651484
As a summary in addition to #Niek's answer. First transpose the data frame by t() by excluding first column (which contains the names and is not numeric and thus cannot used for correlation calculations); assign these names to new columns in same step. Then calculate specific correlations. The solution in one piece would be:
cr2 <- setNames(as.data.frame(t(cr[, -1])), cr[, 1])
with(cr2, cor(Victoria, Nuria, use = "complete.obs"))
[1] 0.3651484
Or for the whole correlation matrix:
cor(cr2, use = "pairwise.complete.obs")

Using geom_text & facet_wrap: Error in unit(x, default.units) : 'x' and 'units' must have length > 0

I am experiencing this weird error..
Some test data:
library(ggplot2)
library(dplyr)
test <- structure(list(group = structure(c(1L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L), .Label = c("G", "N", "P"), class = "factor"),
var = structure(c(1L, 1L, 2L, 3L, 4L, 4L, 4L, 4L, 2L, 2L,
2L, 3L, 2L, 2L, 2L, 5L, 5L, 5L, 4L, 5L, 5L, 5L, 5L, 1L, 1L,
2L, 1L, 1L, 1L, 3L, 3L, 3L, 5L, 3L, 3L, 3L, 4L, 4L, 4L, 1L,
1L, 2L, 2L, 2L, 3L), .Label = c("a", "b", "c", "d", "e"), class = "factor"),
group2 = structure(c(3L, 1L, 3L, 3L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 2L,
3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L,
4L, 1L, 1L, 2L, 3L, 4L), .Label = c("O", "P", "Q", "R"), class = "factor"),
cor = c(0.270075198428616, 0.262097140096646, -0.331312784846655,
-0.343984945812309, 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)), row.names = c(NA, -45L), .Names = c("group", "var",
"group2", "cor"), class = "data.frame")
I'd like to make this plot:
test %>%
ggplot(., aes(x=group2, y=cor)) +
geom_bar(stat="identity", position="dodge", aes(fill=var, group=var)) +
geom_text(aes(label = round(cor,2),
vjust = ifelse(cor >= 0, 0, 1),
group=var),
position = position_dodge(width=1)) +
theme_bw(base_size=18) +
facet_wrap(~group, scales="free_x")
.. which results in: Error in unit(x, default.units) : 'x' and 'units' must have length > 0
However, without geom_text it works:
And also only using rows 1:40 works:
test %>% slice(1:40) %>%
ggplot(., aes(x=group2, y=cor)) +
geom_bar(stat="identity", position="dodge", aes(fill=var, group=var)) +
geom_text(aes(label = round(cor,2),
vjust = ifelse(cor >= 0, 0, 1),
group=var),
position = position_dodge(width=1)) +
theme_bw(base_size=18) +
facet_wrap(~group, scales="free_x")
So up to row 40 there are only two levels for group, and from row 41 on there is a third level. But how can that cause this error? Or is there something else I don't see?
The error also disappears if you erase facet_wrap or if not all of your P-group is NA (for example change the last entry for cor in 0.2). So apparently, if a facet is empty (contains only NA's), geom_text can't handle that.
If you really want to include the empty facet, here's a workaround:
1. replace at least 1 (or all?) NA's in the empty facets with a 0. The bar will have 0 length, so no problem there
2. Now you have a zero label in your graph. By using alpha=ifelse(cor == 0, 0, 1) you'll make that label fully transparent.
Code:
library(ggplot2)
library(dplyr)
test <- structure(list(group = structure(c(1L, 2L, 1L, 1L, 1L, 1L, 1L,
1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 1L,
2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 1L, 1L, 1L, 2L, 2L, 2L,
2L, 3L, 3L, 3L, 3L, 3L), .Label = c("G", "N", "P"), class = "factor"),
var = structure(c(1L, 1L, 2L, 3L, 4L, 4L, 4L, 4L, 2L, 2L,
2L, 3L, 2L, 2L, 2L, 5L, 5L, 5L, 4L, 5L, 5L, 5L, 5L, 1L, 1L,
2L, 1L, 1L, 1L, 3L, 3L, 3L, 5L, 3L, 3L, 3L, 4L, 4L, 4L, 1L,
1L, 2L, 2L, 2L, 3L), .Label = c("a", "b", "c", "d", "e"), class = "factor"),
group2 = structure(c(3L, 1L, 3L, 3L, 1L, 2L, 3L, 4L, 1L,
2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 3L, 4L, 2L,
3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L, 4L, 1L, 2L, 4L, 1L, 2L, 3L,
4L, 1L, 1L, 2L, 3L, 4L), .Label = c("O", "P", "Q", "R"), class = "factor"),
cor = c(0.270075198428616, 0.262097140096646, -0.331312784846655,
-0.343984945812309, 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,
0)), row.names = c(NA, -45L), .Names = c("group", "var",
"group2", "cor"), class = "data.frame")
test %>%
ggplot(., aes(x=group2, y=cor)) +
geom_bar(stat="identity", position="dodge", aes(fill=var, group=var)) +
geom_text(aes(label = round(cor,2),
vjust = ifelse(cor >= 0, 0, 1),
group=var, alpha=ifelse(cor == 0, 0, 1)),
position = position_dodge(width=1)) +
theme_bw(base_size=18) +
facet_wrap(~group, scales="free_x")
If you want to remove the legend of geom_text, simply add show_guide = FALSE.
EDIT: If the transparency doesn't work right, #beetroot came up with this nice alternative: replace alpha=ifelse(cor == 0, 0, 1) with size=ifelse(cor == 0, NA, 3))

Resources