Question

Generate the code to convert the following data frame to wide format. grpsexmeanL sdLmeanR SdR 1A F 0.220.110.340.08 2 A MI 0.47033 0.57033 B F 0.330.11 0.400.0 4B M 0.550.30.650.27 The result should look like the following display. grpF.meanLF.meanRF.sdLF.sdRM.meanLM.meanRM.sdLM.sdR 0.57 0.33 0.33 0.65 0.31 0.27 1A 022 0.34 0.110.08 0.47 0. (), :多 0.40 0.11 0.0 / Hint: use gather() in conjunction with spread() and unite()

0 0
Add a comment Improve this question Transcribed image text
Answer #1

#Loading Libraries
library(tidyr)

#Creating the dataframe
data <- data.frame(grp = c("A", "A", "B", "B"),
sex = c("F", "M", "F", "M"),
meanL = c(0.22, 0.47, 0.33, 0.55),
sdL = c(0.11, 0.33, 0.11, 0.31),
meanR = c(0.34, 0.57, 0.4, 0.65),
sdR = c(0.08, 0.33, 0.07, 0.27))


#Producing the result
new_data <- data %>%
gather("statistics", "value", -c("grp", "sex")) %>%
unite(sex.statistics, c("sex", "statistics"), sep = ".") %>%
arrange(sex.statistics) %>%
spread(key = "sex.statistics", value = "value")

**If result does not match please comment

Add a comment
Know the answer?
Add Answer to:
Generate the code to convert the following data frame to wide format. grpsexmeanL sdLmeanR SdR 1A...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using R, generate the code to convert the following data frame to wide format. 1 AF...

    Using R, generate the code to convert the following data frame to wide format. 1 AF 2 A M 3 BF 4 B M grp Sex neanl sd meanK sdlh 0.34 0.08 0.57 0.33 0.40 0.07 0.65 0.27 0.22 0.11 0.47 0.33 0.33 0.11 0.55 0.31 The result should look like the following display. grp F.meanL F.meanR F.sdL F.sdR M.meanL M.meanR M.sdL M.sdR 0.33 0.27 0.22 0.33 0.34 0.11 0.08 0.400.11 0.07 0.47 0.55 0.57 0.65 0.33 0.31 Hint: use...

  • Use Table 8.1, a computer, or a calculator to answer the following. Suppose a candidate for...

    Use Table 8.1, a computer, or a calculator to answer the following. Suppose a candidate for public office is favored by only 47% of the voters. If a sample survey randomly selects 2,500 voters, the percentage in the sample who favor the candidate can be thought of as a measurement from a normal curve with a mean of 47% and a standard deviation of 1%. Based on this information, how often (as a %) would such a survey show that...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT