crossing two different datasets to mutate and summarise results from one dataset into another
Say I have this fake data frame 
and also have this "external_table" here:
I would like to "synchronize" both datasets to mutate to this external_table a new column, that:
- access the fake_ds
- perform a function into the items from external_table %>% itens_fator
- Mutate the results into the external table
That's the desired output (with fake results) 
That's the function the script should perform:
fake_ds %>% #get my ds
mutate(cronbach_alpha = fake_ds %>% select(external_table, itens_fator) %>% alpha(.)$total[1]) #get variables from external table
If this function is hard to implement, that's ok, you can show me how to apply any other function.
I would like to remain with tidyverse. Thank you
And that's the code to simulate this function:
x<-paste0("y",seq(1:96)) #create X
y<-rep(0:5, 96*2) #create values
fake_ds <- data.frame(x,y) #dataframe
fake_ds %>%
pivot_wider(names_from = x, values_from=y, values_fn = {mean}) -> fake_ds
fake_ds <- fake_ds %>% slice(rep(1:n(), each = 50)) #replicate
fake_ds <- rbind(fake_ds, seq(1:96)) #add variability
#external table
external_table <- structure(list(name = c("X5", "X1", "X2", "X0", "X3", "X4"),
itens_fator = c("y1,y12,y59,y76,y78,y92,y93,y94,y96", "y5,y14,y15,y16,y17,y18,y20,y24,y40,y60,y62,y64,y75",
"y10,y19,y32,y34,y36,y37,y47,y56,y58,y72,y80,y85", "y13,y30,y39,y53,y54,y55,y66,y73,y84,y91",
"y42,y43,y45,y63,y69,y77,y87,y88", "y44,y49,y50,y68,y82,y89"
)), row.names = c(NA, -6L), groups = structure(list(name = c("X0",
"X1", "X2", "X3", "X4", "X5"), .rows = structure(list(4L, 2L,
3L, 5L, 6L, 1L), ptype = integer(0), class = c("vctrs_list_of",
"vctrs_vctr", "list"))), row.names = c(NA, -6L), class = c("tbl_df",
"tbl", "data.frame"), .drop = TRUE), class = c("grouped_df",
"tbl_df", "tbl", "data.frame"))
fake_ds %>% #get my ds
mutate(cronbach_alpha = fake_ds %>% select(external_table, itens_fator) %>% alpha(.)$total[1]) #get variables from external table
from Recent Questions - Stack Overflow https://ift.tt/2G1HxlB
https://ift.tt/3kNUMVR

Comments
Post a Comment