0

I want to calculate the mean of a subset for different participants in a row. I tried a lot of things but there are errors everytime. What is a better approach? I listed below what I tried so far

# calculate positive subscale via mean
positive_subscale <- df_wide_final %>%
  select(GAAIS_Pos_item_1, GAAIS_Pos_item_2, GAAIS_Pos_item_3, GAAIS_Pos_item_4, GAAIS_Pos_item_5, GAAIS_Pos_Item_6, GAAIS_Pos_Item_7, GAAIS_Pos_Item_8, GAAIS_Pos_Item_9, GAAIS_Pos_Item_10, GAAIS_Pos_Item_11, GAAIS_Pos_Item_12)


pos_num <- lapply(positive_subscale, as.numeric)
pos_mean <- rowMeans(pos_num[drop = FALSE])


positive_subscale %>% summarise_each(funs(mean))


mean_pos <- sapply(Filter(is.numeric, positive_subscale), mean)

  
pos_character <- as.character(positive_subscale)
pos_numeric <- as.numeric(pos_character)


mean <- aggregate(positive_subscale$GAAIS_Pos_item_1, mean)

pos_numeric <- as.numeric(unlist(positive_subscale))
rowMeans(pos_numeric, na.rm = TRUE)

gemiddelde_pos <-  mean(strtoi(positive_subscale[1, ]))
lapply(positive_subscale, gemiddelde_pos)

rowMeans(strtoi(positive_subscale)[1, , drop = FALSE])

I tried different approaches to calculate the mean for every participant, but nothing worked so far

wibeasley
  • 5,000
  • 3
  • 34
  • 62
MadriR
  • 1
  • 2
    Welcome to Stack Overflow! Can you please read and incorporate elements from [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269/1082435). Especially the aspects of using `dput()` for the input and then an explicit example of your expected dataset? – wibeasley Jan 08 '23 at 15:48
  • 1
    agree with @wibeasley that it's hard to troubleshoot without a reprex, but just from taking a quick look, if you want to calculate the mean of a subset of some variable, it seems that [dplyr::summarize()](https://dplyr.tidyverse.org/reference/summarise.html) is probably somewhere you could start, especailly combined with [dplyr::group_by()](https://dplyr.tidyverse.org/articles/grouping.html?q=group_by#group_by) – colebrookson Jan 08 '23 at 19:12
  • Please trim your code to make it easier to find your problem. Follow these guidelines to create a [minimal reproducible example](https://stackoverflow.com/help/minimal-reproducible-example). – Community Jan 08 '23 at 19:12

0 Answers0