tpemartin / 110-1-r4ds-main

MIT License
3 stars 73 forks source link

Exercise 4.3 Data frame parsing exercise #33

Open tpemartin opened 3 years ago

tpemartin commented 3 years ago

Data frame parsing exercise:

  1. Declare a list named dfExercise.
dfExercise <- list()

The following feature-by-feature data set (dataSet1) collects name and age of three persons:

dataSet1 <- list(
  name=c("John", "Mary", "Ben"),
  age=c(33, 45, NA)
)
  1. Please parse the data set into a data frame class and add the parsed data frame to dfExercise$data1.

  2. We want to add another feature to dataSet1 called children. We want:

    dataSet1$children[[1]][[1]] # shows the first person's 1st child: name is Jane, age is 2
    dataSet1$children[[2]][[1]] # shows the second person's 1st child: name is Bill, age =3
    dataSet1$children[[2]][[2]] # the second pseron's 2nd child: name is Ken, age=0
    dataSet1$children[[3]][[1]] # the 3rd person's 1st child: name is William, age =10
  3. After adding another feature to dataSet1, parse it to a data frame and save the value at dfExercise$data2.

raychiu135 commented 3 years ago

https://github.com/raychiu135/110-1-r4ds-main/blob/b309570647949751e4f004dd1530ad7775401494/exercise_4.3.rmd#L2

Vincent23412 commented 2 years ago

https://github.com/Vincent23412/110-1-r4ds-main/blob/6073eb167a9d804bbf6184dbbbdf39ba2d6ddaba/20211120.Rmd#L103

tpemartin commented 2 years ago

@raychiu135, @Vincent23412

Please check:

exercise 4.3

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:

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
linttttt commented 2 years ago

https://github.com/linttttt/110-1-r4ds-main/blob/3f4e7a7b9a0242879d5d17eddb9be1ea8962ec99/studyCH4(2).Rmd#L180

Jimmy885 commented 2 years ago

https://github.com/Jimmy885/110-1-r4ds-main/blob/cf32be3c80abd4df194c33d5a91f5ffc5a8ee3c3/4.1%204.2.Rmd#L350

Ann1892 commented 2 years ago

https://github.com/Ann1892/110-1-r4ds-main/blob/8ff83d89659538f577526d7289b81409b6abe3c8/exercises%20fo%20lesson%204.Rmd#L62