Question

Paste the R code for each exercise along with the answers. If: Age <- c(22, 25,...

Paste the R code for each exercise along with the answers.

  1. If:

Age <- c(22, 25, 18, 20)

Name <- c("James", "Mathew", "Olivia", "Stella")

Gender <- c("M", "M", "F", "F")

      then what is the R-code for getting the following output:

##   Age   Name Gender

## 1 22 James      M

## 2 25 Mathew      M

a.

DataFrame = data.frame(c(Age), c(Name), c(Gender))

subset(DataFrame, Gender == "M")

b.

DataFrame = data.frame(c(Age),c(Name),c(Gender))

subset(Gender=="M"), eval=FALSE

c.

DataFrame = data.frame(Age,Name,Gender)

subset(DataFrame,Gender=="M")

d.

DataFrame = data.frame(c(Age,Name,Gender))

subset(DataFrame,Gender=="M")

  1. Create three vectors x,y,z with integers and each vector has 3 elements. Combine the three vectors to become a 3×3 matrix A where each column represents a vector. Change the row names to a,b,c.
  2. mtcars is an inbuilt data set in R.

Use mtcars to create a dataframe: data <- mtcars

Look at the data: data

Extract the data for the cars with mpg greater than 15 but less than 20.

  1. Create a vector x: x<- c(“Student’s” “Are great”)

How many characters are there in x. Hint: use the nchar function.

  1. Create matrices from the vectors below, by binding them column-wise. Then check the mode of the resulting matrix (e.g., character, numeric etc.). What difference do you see and why?
  2. a <- 1:5 ; b <- 1:5
    b. a <- 1:5 ; b <- c('1', '2', '3', '4', '5')
0 0
Add a comment Improve this question Transcribed image text
Answer #1

> #question 1
> Age <- c(22, 25, 18, 20)
> Name <- c("James", "Mathew", "Olivia", "Stella")
> Gender <- c("M", "M", "F", "F")
>
> ## Age Name Gender
> ## 1 22 James M
> ## 2 25 Mathew M
>   
> #a.
> DataFrame = data.frame(c(Age), c(Name), c(Gender))
> subset(DataFrame, Gender == "M")
c.Age. c.Name. c.Gender.
1 22 James M
2 25 Mathew M

> #b.
> DataFrame = data.frame(c(Age),c(Name),c(Gender))
> subset(Gender=="M"), eval=FALSE
Error: unexpected ',' in "subset(Gender=="M"),"
> #c.
> DataFrame = data.frame(Age,Name,Gender)
> subset(DataFrame,Gender=="M")
Age Name Gender
1 22 James M
2 25 Mathew M

> #d.
> DataFrame = data.frame(c(Age,Name,Gender))
> subset(DataFrame,Gender=="M")
c.Age..Name..Gender.
1 22
2 25
5 James
6 Mathew
9 M
10 M

>
> # correct answer c
> # question 2
>
> x<-c(1,2,3)
> y<-c(5,-2,3)
> z<-c(-1,1,2)
> A<-t(rbind(x,y,z))
> row.names(A)<-c("a","b","c")
> A
x y z
a 1 5 -1
b 2 -2 1
c 3 3 2

>
> #question 3
> #Extract the data for the cars with mpg greater than 15 but less than 20.
> data<-mtcars
> data
mpg cyl disp hp drat wt qsec vs am gear carb
Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
Cadillac Fleetwood 10.4 8 472.0 205 2.93 5.250 17.98 0 0 3 4
Lincoln Continental 10.4 8 460.0 215 3.00 5.424 17.82 0 0 3 4
Chrysler Imperial 14.7 8 440.0 230 3.23 5.345 17.42 0 0 3 4
Fiat 128 32.4 4 78.7 66 4.08 2.200 19.47 1 1 4 1
Honda Civic 30.4 4 75.7 52 4.93 1.615 18.52 1 1 4 2
Toyota Corolla 33.9 4 71.1 65 4.22 1.835 19.90 1 1 4 1
Toyota Corona 21.5 4 120.1 97 3.70 2.465 20.01 1 0 3 1
Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
Camaro Z28 13.3 8 350.0 245 3.73 3.840 15.41 0 0 3 4
Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
Fiat X1-9 27.3 4 79.0 66 4.08 1.935 18.90 1 1 4 1
Porsche 914-2 26.0 4 120.3 91 4.43 2.140 16.70 0 1 5 2
Lotus Europa 30.4 4 95.1 113 3.77 1.513 16.90 1 1 5 2
Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6
Maserati Bora 15.0 8 301.0 335 3.54 3.570 14.60 0 1 5 8
Volvo 142E 21.4 4 121.0 109 4.11 2.780 18.60 1 1 4 2

> data[(data[,1]>15)&(data[,1]<20),]
mpg cyl disp hp drat wt qsec vs am gear carb
Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
Merc 280C 17.8 6 167.6 123 3.92 3.440 18.90 1 0 4 4
Merc 450SE 16.4 8 275.8 180 3.07 4.070 17.40 0 0 3 3
Merc 450SL 17.3 8 275.8 180 3.07 3.730 17.60 0 0 3 3
Merc 450SLC 15.2 8 275.8 180 3.07 3.780 18.00 0 0 3 3
Dodge Challenger 15.5 8 318.0 150 2.76 3.520 16.87 0 0 3 2
AMC Javelin 15.2 8 304.0 150 3.15 3.435 17.30 0 0 3 2
Pontiac Firebird 19.2 8 400.0 175 3.08 3.845 17.05 0 0 3 2
Ford Pantera L 15.8 8 351.0 264 4.22 3.170 14.50 0 1 5 4
Ferrari Dino 19.7 6 145.0 175 3.62 2.770 15.50 0 1 5 6

>
>
> #question 4
> x<-c("Student's","Are great")
> sum(nchar(x))
[1] 18
>
> # question 5
> #Create matrices from the vectors below, by binding them column-wise. Then check the mode of the resulting matrix (e.g., character, numeric etc.). What difference do you see and why?
> a <- 1:5
> b <- 1:5
> cbind(a,b)
a b
[1,] 1 1
[2,] 2 2
[3,] 3 3
[4,] 4 4
[5,] 5 5

> a <- 1:5
> b <- c('1', '2', '3', '4', '5')
> cbind(a,b)
a b
[1,] "1" "1"
[2,] "2" "2"
[3,] "3" "3"
[4,] "4" "4"
[5,] "5" "5"
> #here the diffrence is that in the first case it is a matrix containing integers and in the second case it contains characters
> # it was because in 2nd case b is a array of characters.

R CODE

#question 1
Age <- c(22, 25, 18, 20)
Name <- c("James", "Mathew", "Olivia", "Stella")
Gender <- c("M", "M", "F", "F")

## Age Name Gender
## 1 22 James M
## 2 25 Mathew M
  
#a.
DataFrame = data.frame(c(Age), c(Name), c(Gender))
subset(DataFrame, Gender == "M")
#b.
DataFrame = data.frame(c(Age),c(Name),c(Gender))
subset(Gender=="M"), eval=FALSE
#c.
DataFrame = data.frame(Age,Name,Gender)
subset(DataFrame,Gender=="M")
#d.
DataFrame = data.frame(c(Age,Name,Gender))
subset(DataFrame,Gender=="M")

# correct answer c
# question 2

x<-c(1,2,3)
y<-c(5,-2,3)
z<-c(-1,1,2)
A<-t(rbind(x,y,z))
row.names(A)<-c("a","b","c")
A

#question 3
#Extract the data for the cars with mpg greater than 15 but less than 20.
data<-mtcars
data
data[(data[,1]>15)&(data[,1]<20),]


#question 4
x<-c("Student's","Are great")
sum(nchar(x))

# question 5
#Create matrices from the vectors below, by binding them column-wise. Then check the mode of the resulting matrix (e.g., character, numeric etc.). What difference do you see and why?
a <- 1:5
b <- 1:5
cbind(a,b)
a <- 1:5
b <- c('1', '2', '3', '4', '5')
cbind(a,b)
#here the diffrence is that in the first case it is a matrix containing integers and in the second case it contains characters
# it was because in 2nd case b is a array of characters.

Add a comment
Know the answer?
Add Answer to:
Paste the R code for each exercise along with the answers. If: Age <- c(22, 25,...
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
  • How would you write code in R studio for: Step 2: In a new code chunk...

    How would you write code in R studio for: Step 2: In a new code chunk below the last, write code that will create a subset of the data containing just the name, height and weight columns for males that are no more than 48" tall (Southpark fitness data) or no more than 68" tall (your own lab 1 fitness data). Step 3: Below the last code, write another command that will create a dataframe with just the name, time,...

  • This question needs to be solved in R: Run the following code to generate a data...

    This question needs to be solved in R: Run the following code to generate a data frame called `ucb`. ```{r,eval=TRUE} rm(list=ls()) gender = rep(c("female","male"),c(1835,2691)) admitted = rep(c("yes","no","yes","no"),c(557,1278,1198,1493)) dept = rep(c("A","B","C","D","E","F","A","B","C","D","E","F"), c(89,17,202,131,94,24,19,8,391,244,299,317)) dept2 = rep(c("A","B","C","D","E","F","A","B","C","D","E","F"), c(512,353,120,138,53,22,313,207,205,279,138,351)) department = c(dept,dept2) ucb = data.frame(gender,admitted,department) rm(gender,admitted,dept,dept2,department) ls() ``` a) Does there seem to be a relationship between department and gender? Draw your conclusion based on two approaches: (1) use an appropriate plot, and (2) a parametric test. For the parametric test, **write down the...

  • R code all steps (copy paste it) - for all answers . R problem 1: Cernsus...

    R code all steps (copy paste it) - for all answers . R problem 1: Cernsus At School is a project that engages students in grades 4 - 12. The data from the project contains many variables. In this problem we will examine the following variables: -Languages.spoken: the number of languages a student can hold an everyday conversation Armspan.cm: the physical measurement of the length from one end of a student's arms (measured at the fingertips) to the other when...

  • What is the R code and result in Rstudio 1. The following data gives, for each...

    What is the R code and result in Rstudio 1. The following data gives, for each amount by which an elastic band is stretched over the end of a ruler, the distance that the band moved when released: stretch 46 54 48 50 44 4 52 distance 148 182 173166 109 141 166 Create a data frame in R with two columns that contain "stretch and "dis tance" respectively. Plot the distance versus the stretch using plot) function What trend...

  • Question 2 If you read in a csv file using read.csv() function into R, what is the resulting datastructure in which R st...

    Question 2 If you read in a csv file using read.csv() function into R, what is the resulting datastructure in which R stores the read-in data? A. numeric B. matrix    C. data.frame    D. vector    Question 3 Suppose you have 4 integers, 4 characters, and 4 logical values. Which datastructure can you use to store all 12 values? Choose one or more options. A. a vector B. a matrix C. a list D. a data frame Question 4 Suppose you have...

  • STATS Use the R function rnorm() to simulate selecting a random sample of size 25 from...

    STATS Use the R function rnorm() to simulate selecting a random sample of size 25 from a population with mean 80 and s.d. 20. The goal here is to show how contamination affects the mean, s.d., and z-scores. (a) Obtain the sample mean and sample sd of the simulated sample and use them to obtain the z-score for 100. (b) Create the vector contam = c(0,seq(1000,10000,length=21)) To show the effects of contamination, separately add each value of contam to the...

  • Read the R help file for geom_histogram(). Fill in the blanks with the function of each...

    Read the R help file for geom_histogram(). Fill in the blanks with the function of each argument using the information under ‘usage’ and ‘arguments’ to better understand the different parts needed to run geom_histogram(). Options for A, B, C, D are: 1. Aesthetic mapping (which columns to use) 2. Data Frame 3. Name of Row 4.Number of bins 5. Units of Y-axis 6. Vector 7.Aesthetic Mapping (which rows to use) geom histogram(data - mtcars, aesx-mpg), bins 20, stat count"

  • 1. For each of the following regression models, write down the X matrix and 3 vector....

    1. For each of the following regression models, write down the X matrix and 3 vector. Assume in both cases that there are four observations (a) Y BoB1X1 + B2X1X2 (b) log Y Bo B1XiB2X2+ 2. For each of the following regression models, write down the X matrix and vector. Assume in both cases that there are five observations. (a) YB1XB2X2+BXE (b) VYBoB, X,a +2 log10 X2+E regression model never reduces R2, why 3. If adding predictor variables to a...

  • I think the lambdas are (0, 1, -1) then how can I solve (b), (c) ?? Thank YOU! 5. [20pts] Suppose A is a 3x3 matrix wit...

    I think the lambdas are (0, 1, -1) then how can I solve (b), (c) ?? Thank YOU! 5. [20pts] Suppose A is a 3x3 matrix with independent eigenvector x,x2,x, satisfying Ay bx, -cx for any vector y ax +bx, +cx, in R (a) What is the rank of A? What are the eigenvalues of A? Describe all vectors in its column space C(A) T (b) How would you solve du/dt Au with u(0) (1, 1, 1) ? (c) What...

  • 5. 3. State the magnitude and direction of each of the vectors given below. a) r--30...

    5. 3. State the magnitude and direction of each of the vectors given below. a) r--30 m (displacement vector) b) v 60 m/s west (velocity vector) c) F 20 N at-45° (force vector) d) p50 kg m/s at 25° (linear momentum vector) 4. Provide a graphical example of a 1-dimensional vector (ID) and one of a 2- dimensional vector (2D). Be sure to include reference axes with labels in each case. 5. 1D Vectors. Let vector A +3 units and...

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