library(tibble)
<- dplyr::band_instruments2 a a
Output:
# A tibble: 3 × 2
artist plays
<chr> <chr>
1 John guitar
2 Paul bass
3 Keith guitar
deframe(a)
Output:
John Paul Keith "guitar" "bass" "guitar"
Opposite to enframe()
, the function deframe()
converts a two-column tibble to a named vector or list, using the first column as name and the second column as value. If the input tibble has only one column, an unnamed vector is returned.
e.g.1. Turn a two-column tibble to a named vector.
library(tibble)
<- dplyr::band_instruments2 a a
Output:
# A tibble: 3 × 2
artist plays
<chr> <chr>
1 John guitar
2 Paul bass
3 Keith guitar
deframe(a)
Output:
John Paul Keith "guitar" "bass" "guitar"
e.g.2. Turn two-columns to a named list.
# create a tibble containing list-columns<- tibble( students Student = c("Alice", "Bob", "Charlie"), Courses = list( c("Math", "Science", "History"), c("English", "Math", "Art"), c("Science", "History", "Geography") ), Grades = list( c(90, 85, 80), c(88, 92, 85), c(85, 87, 90) )) students
Output:
# A tibble: 3 × 3
Student Courses Grades
<chr> <list> <list>
1 Alice <chr [3]> <dbl [3]>
2 Bob <chr [3]> <dbl [3]>
3 Charlie <chr [3]> <dbl [3]>
# turn the first two columns into a named listdeframe(students[, 1:2])
Output:
$Alice
[1] "Math" "Science" "History" $Bob
[1] "English" "Math" "Art" $Charlie
[1] "Science" "History" "Geography"
# turn the second and third column to a named listdeframe(students[, 2:3])
Output:
$`c("Math", "Science", "History")`
[1] 90 85 80 $`c("English", "Math", "Art")`
[1] 88 92 85 $`c("Science", "History", "Geography")`
[1] 85 87 90