简体   繁体   中英

In R, how to extract multiple variables from a column and create new variable

I have a dataset that contains multiple variables within one column查看数据集. I am trying to figure out how to extract the variables (6 unique questions) from column 'question' with their associated value in percentage of respondents. When I pivot wider, it doesn't return the results I need (image attachedpivot_wider 的输出)

My ideal output is a mean percentage of respondents, grouped by country and sex for each question.

ie an output that has Country: Afghanistan, year, mean percentage of respondents per question.

dff2 <- df2 %>% pivot_wider(names_from = 'Question', values_from = 'Percentage of respondents')

thank-you!

you need to dcast your data.

from reshape2 package, use dcast() function.

Here how to use it:

dcast(data, Country + Sex ~ Question, value.var = 'Percentage of respondents')

Use the names of without strings quotes ""

在此处输入图片说明

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM