R Data Frames: Create, Access, and Subset

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which of the following best describes a data frame in R?

  • A generic data object used for storing tabular data, similar to a spreadsheet or SQL table. (correct)
  • A specialized type of list that can only hold numeric data.
  • A statistical model for performing regression analysis.
  • A function used to create plots and charts from data.

Given the following R code: vec1 = c(1, 2, 3) vec2 = c("R", "Scilab", "Java") df = data.frame(vec1, vec2) What is the structure of the resulting df?

  • A single vector containing all the values from `vec1` and `vec2`.
  • A data frame with two columns: `vec1` (numeric) and `vec2` (character). (correct)
  • A list with two elements, `vec1` and `vec2`, each containing their original values.
  • A matrix with two columns, one containing numeric values and the other containing character strings .

When creating a data frame from a file, which separator should be used in the read.table() function if the data entries are separated by commas?

  • `sep = "|"`
  • `sep = ","` (correct)
  • `sep = " "`
  • `sep = "\t"`

In R, what does df[1:2,] refer to when accessing elements of a data frame df?

<p>The first two rows and all columns of <code>df</code>. (C)</p> Signup and view all the answers

What will be the output of the following R code?

pd <- data.frame(
 Name = c("Senthil","Senthil","Sam","Sam"),
 BS = c(141.2,139.3,135.2,160.1)
)
pd2 <- subset(pd, Name == "Senthil" | BS > 150)
print(pd2)

<p>A data frame containing rows where <code>Name</code> is &quot;Senthil&quot; OR <code>BS</code> is greater than 150. (C)</p> Signup and view all the answers

Given a data frame df, what is the purpose of the following R code? df[[2]][2] <- "NewValue"

<p>It replaces the second element in the second column of <code>df</code> with the string &quot;NewValue&quot;. (D)</p> Signup and view all the answers

Which R command allows you to interactively edit a dataframe in a spreadsheet-like interface?

<p><code>edit()</code> (B)</p> Signup and view all the answers

What functions can be used, respectively, to add extra rows and extra columns to a data frame in R?

<p><code>rbind</code> and <code>cbind</code> (C)</p> Signup and view all the answers

Given a data frame df, what does the following code do? df2 <- df[-3, -1]

<p>It creates a new data frame <code>df2</code> excluding the third row and the first column of <code>df</code>. (C)</p> Signup and view all the answers

If you want to remove the column named "Salary" from a data frame called employees, which of the following R code snippets would achieve this?

<p>All of the above (D)</p> Signup and view all the answers

What is the primary issue that arises when manipulating character columns in a data frame after its creation, and how can it be resolved?

<p>Factor level mismatch; resolved by setting <code>stringsAsFactors = FALSE</code> when creating the data frame. (D)</p> Signup and view all the answers

In R, if you encounter NA values after assigning a character string to a factor column in a dataframe, what is the most likely cause?

<p>The string is not one of the existing levels in the factor column. (D)</p> Signup and view all the answers

How can we ensure the dataframe does not automatically convert the strings into factors?

<p>Setting <code>stringsAsFactors = FALSE</code> when initiating the dataframe. (A)</p> Signup and view all the answers

Given the code:

df <- data.frame(col1 = c(1, 2, 3), col2 = c("A", "B", "C"), stringsAsFactors = TRUE)
df[2, 2] <- "D"
print(df)

What will print(df) output?

<p>A dataframe where element (2,2) is <code>NA</code> with a warning message. (B)</p> Signup and view all the answers

With stringsAsFactors = FALSE, what is the output of the following code?

df <- data.frame(col1 = c(1, 2, 3), col2 = c("A", "B", "C"), stringsAsFactors = FALSE)
df[2, 2] <- "D"
print(df)

<p>A dataframe where element (2,2) is 'D'. (D)</p> Signup and view all the answers

Flashcards

What are Data Frames?

Generic data objects in R used to store tabular data.

What does read.table() do?

Reads data from a file to create a dataframe

What is subset()?

Extracts a subset of data based on specified conditions.

What does df[val1, val2] refer to?

Refers to a specific row and column in a data frame using their index or name.

Signup and view all the flashcards

What does rbind() do?

Adds extra rows to a dataframe.

Signup and view all the flashcards

What does cbind() do?

Adds extra columns to a dataframe.

Signup and view all the flashcards

What is the edit() function used for?

Used to manually edit a dataframe in a table editor.

Signup and view all the flashcards

What does df[-3,-1] do?

Removes rows or columns from a dataframe based on index or conditions.

Signup and view all the flashcards

What does stringsAsFactors = F do?

Specifies that character columns should not be converted to factors when creating a data frame.

Signup and view all the flashcards

Study Notes

  • Data frames are generic data objects of R, used for storing tabular data.

Creating Data Frames

  • Data frames can be created by reading data from a file
  • Use '/' instead of '' for file paths
  • Example file path: "C:/Users/hii/Documents/R/R-Workspace/"
  • A separator can distinguish between entries; the default separator is space.
  • Command to read a file: newDF = read.table(path="Path of the file")
  • Including a separator: newDF = read.table(file="path of the file", sep)

Accessing Rows and Columns

  • df[val1,val2] refers to row “val1” and column “val2”, which can be number or string
  • "val1" or "val2" can be an array of values like "1:2" or "c(1,3)"
  • df[val2] (no commas) refers to column “val2” only

Subsetting

  • The subset() function extracts a subset of data based on conditions.
  • Example:
    • pd=data.frame("Name"=c("Senthil","Senthil","Sam","Sam"), "Month"=c("Jan","Feb","Jan","Feb"), "BS" = c(141.2,139.3,135.2,160.1), "BP" = c(90,78,80,81))
    • pd2 = subset(pd,Name=="Senthil" | BS> 150 )

Editing

  • Data frames can be edited by direct assignment
  • A data frame can be edited using the edit() command
  • Create an instance of a data frame and use the edit command to manually open a table editor to change values

Adding Rows and Columns

  • Extra rows can be added with the function rbind
  • Extra columns can be added with the function cbind
  • Example:
    • df = rbind(df,data.frame(vec1=4, vec2="C", vec3="For Scaleup"))
    • df = cbind(df,vec4=c(10,20,30,40))

Deleting Rows and Columns

  • Rows and columns can be deleted in multiple ways
  • A '-' sign before the value and before ',' deletes rows
  • A '-' sign before the value and after ',' deletes columns
  • ! means no to specified rows/columns satisfying the condition

Manipulating Rows - Factor Issue

  • Character columns become factors when created in a data frame
  • Factor variables are character columns split into categories or factor levels
  • When character columns are created in a data frame, they become factors
  • New entries are to be consistent with factor levels fixed when the dataframe is first created

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

Quiz de Pandas
3 questions

Quiz de Pandas

LikedMossAgate avatar
LikedMossAgate
Quiz giorno 1
17 questions

Quiz giorno 1

DeliciousSynergy4717 avatar
DeliciousSynergy4717
Pandas DataFrames and Data Manipulation
32 questions
R Data Structures: Vectors and Data Frames
43 questions
Use Quizgecko on...
Browser
Browser