Question

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 gather) in conjunction with spread(

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

> data1=read.csv(file.choose(),header=T)
> data1
grp sex meanL sdL meanR sdR

1 A F 0.22 0.11 0.34 0.08

2 A M 0.47 0.33 0.57 0.33

3 B F 0.33 0.11 0.40 0.07

4 B M 0.55 0.31 0.65 0.27


#convert data long to wide data
> data2=reshape(data1,idvar="grp",timevar="sex",direction="wide")
> data2
grp meanL.F sdL.F meanR.F sdR.F meanL.M sdL.M meanR.M sdR.M

1 A 0.22 0.11 0.34 0.08 0.47 0.33 0.57 0.33

3 B 0.33 0.11 0.40 0.07 0.55 0.31 0.65 0.27

#find column name
> colnames(data2)
[1] "grp" "meanL.F" "sdL.F" "meanR.F" "sdR.F" "meanL.M" "sdL.M" "meanR.M"
[9] "sdR.M"  

#reorder the column for given condition
> col_order=c(grp,"meanL.F","meanR.F","sdL.F","sdR.F","meanL.M","meanR.M","sdL.M","sdR.M")
> data3=data2[,col_order]
> data3
  
grp meanL.F meanR.F sdL.F sdR.F meanL.M meanR.M sdL.M sdR.M

1 A 0.22 0.34 0.11 0.08 0.47 0.57 0.33 0.33

2 B 0.33 0.40 0.11 0.07 0.55 0.65 0.31 0.27

>
#rename column name for given condition

> names(data3)[names(data3)=="meanL.F"]="F.meanL"
> names(data3)[names(data3)=="meanR.F"]="F.meanR"
> names(data3)[names(data3)=="sdL.F"]="F.sdL"
> names(data3)[names(data3)=="sdR.F"]="F.sdR"
> names(data3)[names(data3)=="meanL.M"]="M.meanL"
> names(data3)[names(data3)=="meanR.M"]="M.meanR"
> names(data3)[names(data3)=="sdL.M"]="M.sdL"
> names(data3)[names(data3)=="sdR.M"]="M.sdR"
> data3

#final result

grp F.meanL F.meanR F.sdL F.sdR M.meanL M.meanR M.sdL M.sdR

1 A 0.22 0.34 0.11 0.08 0.47 0.57 0.33 0.33

2 B 0.33 0.40 0.11 0.07 0.55 0.65 0.31 0.27

>

Add a comment
Know the answer?
Add Answer to:
Using R, generate the code to convert the following data frame to wide format. 1 AF...
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
  • Generate the code to convert the following data frame to wide format. grpsexmeanL sdLmeanR SdR 1A...

    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()

  • 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...

  • there are 3 parts to this question. The weight is 100 grams. [Q2-Home/Lab...) We attach a...

    there are 3 parts to this question. The weight is 100 grams. [Q2-Home/Lab...) We attach a known weight (~100 gr) to a spring. By pulling down the spring by no more a couple of cm put it in oscillating mode. This experiment is described here [the screencast can be played in the PH 114-LABORATORY MANUAL, OSCILLALTIONS, UAH PHYSICS 2020-2021 classroom in mute as long as you keep an eye to spot the information you need]. Get the data from this...

  • A fnance executve would ike to determine if a relationship exists between the current earnings per share (EPS) of...

    A fnance executve would ike to determine if a relationship exists between the current earnings per share (EPS) of a bank and the total assets S billions), previous period's EPS, previous period's retun on average assets (ROAA), and the previous period's retun on average equity (ROAE) ROAA measures how effectively assets are utilized, and ROAE measures a firm's profitability. Complete parts a through d below EClick the icon to view the table of data. a. Construct a regression model using...

  • Suppose 1000 coins are tossed. Use the normal curve approximation to the binomial distribution to find...

    Suppose 1000 coins are tossed. Use the normal curve approximation to the binomial distribution to find the probability of getting the following result. Exactly 495 heads Use the table of areas under the standard normal curve given below. Click here to view page 1. Click here to view page 2. Click here to view page 3. Click here to view page 4. Click here to view page 5. Click here to view page 6. The probability of getting exactly 495...

  • Suppose 16 coins are tossed. Use the normal curve approximation to the binomial distribution to find...

    Suppose 16 coins are tossed. Use the normal curve approximation to the binomial distribution to find the probability of getting the following result. More than 11 tails. Use the table of areas under the standard normal curve given below. Click here to view page 1. Click here to view page 2. Click here to view page 3. Click here to view page 4. Click here to view page 5. Click here to view page 6. Binomial probability = (Round to...

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