Consider the CSV file data.csv with content:
name,age Alice,30 Bob,25
What will be the output of this R code?
df <- read.csv('data.csv') print(df$age[2])
Remember that read.csv reads the file into a data frame and $ accesses a column.
The second row's age is 25, so df$age[2] prints 25.
Given this R code:
df <- data.frame(name=c('Tom','Jerry'), score=c(88, 92))
write.csv(df, 'scores.csv', row.names=FALSE)What will the content of scores.csv be?
By default, write.csv adds quotes around strings and includes header.
With row.names=FALSE, row numbers are not written. Strings are quoted by default.
What error does this code produce and why?
df <- read.csv('data.csv', header=FALSE)
print(df$age)Assume data.csv has a header row with columns name and age.
df <- read.csv('data.csv', header=FALSE) print(df$age)
When header=FALSE, column names default to V1, V2, ....
Without header, columns are named V1, V2, so df$age is NULL.
Which of these write.csv calls is syntactically invalid?
Check commas between arguments.
Option D misses a comma between arguments, causing syntax error.
Given a CSV file data.csv with 5 rows of data plus 1 header row, what is the number of rows in df after this code?
df <- read.csv('data.csv', skip=2)df <- read.csv('data.csv', skip=2)
The skip argument skips lines before reading header.
skip=2 skips the header and first data row. Then, since header=TRUE (default), the next line is treated as header, leaving 3 data rows.