Transforming variables (I think)

Hi there! New to R here and really needing some help! I've been at this for a week :upside_down_face:

First of all, here is my data:

dput(head(merged_data_ctrl_tbl))

So! I would like to make a bar chart in order to compare the means of some data I have gathered. So far I have one big dataset that contains my entire corpus (which contains documents from 4 major newspapers). They are separated in a column entitled "newspaper" where I have assigned the correct newspaper name to the correct document. I also have 5 columns that correspond to the frequency of foundation words found in each document. There are 5 foundations: Care, Fairness, Authority, Loyalty and Sanctity. I hope you are still baring with me! I'm still not sure how to explain it succintly.
Each document has a frequency for each foundation. I have managed to describe the data in order to find out the Mean value of the frequency of each foundation word by newspaper, and I would like this in a bar chart. I know how to construct one, but I do not know how to turn my foundations into one categorical variable that I can then use the fill() function with "newspaper" in order to get a general comparison. How do I do this?

I tried this:

df_2 <- pivot_longer(data = merged_data_ctrl_tbl, cols = carePer100Words:sanctityPer100Words, names_to = "Foundation", values_to = "Mean")
df_2

ggplot(data = df_2) +
  geom_bar(mapping = aes(x = Foundation, fill= newspaper), position= "dodge")

But it only gave me the Mean of each document, not overall per newspaper, and when put into a bar chart, it only gave me the count of each. Not very interesting! I also only want the variables that say "Per100Words" and don't know how to exclude those that say "PerWord". I know I'm close but my brain isn't working and I have no idea how to create the new data set. I'm so sorry if I've written this badly, I'm not yet fluent in R Studio :slight_smile:

Thank you immensely for any help possible, it means the world to me!

You are supposed to post the output of that command, not the command itself.

To help us help you, could you please prepare a reproducible example (reprex) illustrating your issue? Please have a look at this guide, to see how to create one:

This topic was automatically closed 21 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.