0% found this document useful (0 votes)
5 views3 pages

Data Frame Manipulation Exercises

The document provides a series of exercises related to data frames in R, including creating and manipulating data frames, adding columns, and importing data from files. It covers tasks such as adding age and weight loss columns, reordering columns, and extracting specific records based on conditions. Additionally, it involves constructing a data frame for student points and assigning grades based on performance percentiles.

Uploaded by

anbinh05010601
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views3 pages

Data Frame Manipulation Exercises

The document provides a series of exercises related to data frames in R, including creating and manipulating data frames, adding columns, and importing data from files. It covers tasks such as adding age and weight loss columns, reordering columns, and extracting specific records based on conditions. Additionally, it involves constructing a data frame for student points and assigning grades based on performance percentiles.

Uploaded by

anbinh05010601
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Exercises - Data Frames

1. Create a data frame named dframe in accordance with the following table.
Note, person should be a vector of strings, while sex and funny should be
factors with the observed nominal levels and ordinal levels, respectively.

Now do or find the following:


a. Add an age column where the ages of Stan, Francine, Steve, Roger,
Hayley, and Klaus are 41, 41, 15, 1600, 21, and 60, respectively.
(Yes, Roger is very old.)
b. Reorder the columns of the data frame so that they occur in the
following order: person, age, sex, funny.
c. Make a new data frame similarly constructed and named dframe2 in
accordance with the table below.

Then combine these two data frames into a single data frame called mydataframe.
d. Write a single line of code that will extract from mydataframe just
the names and ages of any records where the individual is female
and has a level of funniness of "Med" or "High".
2. A data frame named diet has components gender (M or F), age (an
integer), [Link] (diet or placebo), [Link] (possibly a
decimal value), and [Link] (also possibly a decimal value).
a. Write a statement in R that adds a component [Link] to the data
frame diet whose values are the amounts of weight each subject lost
during the program.
b. Describe what the following code will do:
tapply(diet$[Link], diet$[Link], hist)
3. Import the Excel file "[Link]" into R as a data frame named apples.
4. Import the text file [Link] into R as a data frame named people.
5. After creating the data frame people above, display the age and birth month
(in that order) of all males in the dataset.
6. Points earned for several students are given below. Construct a data frame
to hold this data, and then add a column to that data frame
called grade where the grades are assigned in the following way: A for the
top 10% of point totals, B for the next 20%, C for the middle 40%, D for
the 20% below that, and F for the bottom 10%.
studentId pts
1 86
2 84
3 81
4 90
5 85
6 79
7 79
8 75
9 66
10 86
11 73
12 84
13 83
14 68
15 78
16 93
17 82
18 75
19 80
20 87
21 82
22 66
23 91
24 79
25 81

Common questions

Powered by AI

To filter a data frame in R based on conditionals involving categorical variables, use the `subset()` function or logical indexing. For example, to extract names and ages of females with 'Med' or 'High' funniness from `mydataframe`, you can use `subset(mydataframe, sex == 'Female' & funny %in% c('Med', 'High'), select = c(person, age))` .

An efficient way to display specific columns for selected rows based on conditions is to use logical indexing or the `dplyr` package. Using base R, `subset()` could be employed, e.g., `subset(people, gender == 'M', select = c(age, birth_month))`, to show 'age' and 'birth month' for males .

Excel files can be converted to data frames in R using the `readxl` package and the `read_excel` function, while text files can be converted using `read.table` or `read.csv`. This conversion enables structured data manipulation and analysis leveraging R's extensive statistical, graphical, and data manipulation capabilities, critical for efficient data analysis workflows .

To import an Excel file into R as a data frame, you can use the `readxl` package with the `read_excel` function. For instance, `apples <- read_excel("mydata.xlsx")` imports the Excel file 'mydata.xlsx' into a data frame named `apples` .

Factors in R are data structures used for fields that have a fixed number of potential values (categories or levels). They differ from character vectors as factors store categorical data more efficiently and enable statistical modeling capabilities that interpret them correctly as specific statistical structures, not just strings. Factors are useful for statistical analysis and plotting .

The `tapply()` function in R is used to apply a function to each element of a list-like object, taking subsets defined by a factor or another vector. It is applicable for actions like creating plots for different groups. In the scenario given, `tapply(diet$weight.loss, diet$treatment.group, hist)` applies `hist()` to `weight.loss` for each `treatment.group`, producing separate histograms for each group .

The `tapply()` function in R applies a function to subsets of a vector defined by another vector. In `tapply(diet$weight.loss, diet$treatment.group, hist)`, it creates histograms of `weight.loss` for each `treatment.group`. The histograms show the distribution of weight loss within the 'diet' and 'placebo' groups separately .

You can programmatically assign grades by first sorting the points, determining the score cut-offs using the `quantile()` function, and then using conditional indexing to assign grades. After creating a data frame with student points, use quantiles to define grade boundaries and use `ifelse()` or `case_when()` for assignment. For example, using quantiles to split the data into A, B, C, D, F can automate the grading .

To add an age column to a data frame in R, you can directly assign a vector to a new column in the existing data frame. For example, use `dframe$age <- c(41, 41, 15, 1600, 21, 60)` to add ages to 'Stan', 'Francine', 'Steve', 'Roger', 'Hayley', and 'Klaus', respectively. To reorder the columns, you can use the indexing method to select columns in the desired order, for example, `dframe <- dframe[, c("person", "age", "sex", "funny")]` .

To combine two data frames with similar structures into a single data frame in R, you can use the `rbind()` function, which concatenates them by rows. For example, `mydataframe <- rbind(dframe1, dframe2)` would append the rows of `dframe2` to `dframe1`, assuming both have the same columns .

You might also like