Open tpemartin opened 3 years ago
@raychiu135, @Vincent23412
Please check:
dfExercise <- list()
dataSet1 <- list(
name=c("John", "Mary", "Ben"),
age=c(33, 45, NA)
)
# 2
dfExercise$data1 <-
data.frame(
dataSet1
)
# 3
dataSet1$children[[1]] <- list(
list(name="Jane", age=2))
dataSet1$children[[2]] <- list(
list(name="Bill", age=3),
list(name="Ken", age=0))
dataSet1$children[[3]] <- list(
list(name="William", age=10))
# 4
dfExercise$data2 <- list2DF(dataSet1)
Declare-and-add method on list when creating a nested list using multiple chained-retrievals, there is a rule to remember:
[[]]
is used, then it must be at the last. l0 <- list() # declare
l0[[1]]$name <- "John" # won't work
l0$name[[1]] <- "John" # work
l0$secondPerson$name <- "Jane" # work
l0$secondPerson$children[[1]] <- "Albigail" # work
l0$secondPerson[[3]]$habit <- "tennis" # won't work
Data frame parsing exercise:
The following feature-by-feature data set (dataSet1) collects name and age of three persons:
Please parse the data set into a data frame class and add the parsed data frame to dfExercise$data1.
We want to add another feature to dataSet1 called children. We want:
After adding another feature to dataSet1, parse it to a data frame and save the value at dfExercise$data2.