tibble: Simple Data Frames
- CRAN: http://cran.r-project.org/web/packages/tibble/index.html
- GitHub: https://github.com/hadley/tibble
- Vignettes: Data frames and table sources in "dplyr" style
> library(tibble)
バージョン: 1.3.1
関数名 | 概略 |
---|---|
add_column |
Add columns to a data frame |
add_row |
Add rows to a data frame |
as_tibble |
Coerce lists and matrices to data frames. |
enframe |
Converting atomic vectors to data frames, and vice versa |
frame_matrix |
Row-wise matrix creation |
glimpse |
Get a glimpse of your data. |
has_name |
Does an object have an element with this name? |
is.tibble |
Test if the object is a tibble. |
lst |
Build a list |
rownames |
Tools for working with row names |
set_tidy_names |
Repair object names. |
tibble-package |
tibble: Simple Data Frames |
tribble |
Row-wise tibble creation |
add_row
行の追加
> (df <- data_frame(x = 1:3, y = 3:1))
# A tibble: 3 x 2
x y
<int> <int>
1 1 3
2 2 2
3 3 1
> add_row(df, x = 4, y = 0)
# A tibble: 4 x 2
x y
<dbl> <dbl>
1 1 3
2 2 2
3 3 1
4 4 0
all_equal
データフレーム構造の比較
ref) base::all.equal
, dplyr::add_equal
> mtcars_df <- as_data_frame(mtcars)
> tibble:::all_equal(mtcars_df, mtcars_df)
Error in get(name, envir = asNamespace(pkg), inherits = FALSE): object 'all_equal' not found
> tibble:::all_equal(mtcars_df, mtcars)
Error in get(name, envir = asNamespace(pkg), inherits = FALSE): object 'all_equal' not found
as_data_frame
リスト、行列クラスのオブジェクトを強制的にデータフレームクラスのオブジェクトへと変換する
> l <- list(x = 1:500, y = runif(500), z = 500:1)
> (df <- as_data_frame(l))
# A tibble: 500 x 3
x y z
<int> <dbl> <int>
1 1 0.117581485538 500
2 2 0.761716070119 499
3 3 0.141795899253 498
4 4 0.766908397898 497
5 5 0.842138030799 496
6 6 0.776749359677 495
7 7 0.133827658836 494
8 8 0.805936092744 493
9 9 0.896782782627 492
10 10 0.926979867974 491
# ... with 490 more rows
> m <- matrix(rnorm(50), ncol = 5)
> (df <- as_data_frame(m))
# A tibble: 10 x 5
V1 V2 V3 V4
<dbl> <dbl> <dbl> <dbl>
1 -0.429531502079 -0.665818408586 0.1017221916258 -0.3067937000311
2 0.131579552282 1.445858792643 -0.2889908762118 1.7048838403867
3 -0.130438965975 0.791653292314 0.1395242777675 2.0223794317045
4 0.731357619792 2.380875723873 -0.4393347992880 -0.0855216352114
5 -0.115707706209 1.694230708376 -0.3905204582614 1.2936075877152
6 0.651926695900 -1.836646051013 -0.8069781845554 -0.6028943723889
7 -0.115131121322 0.321954472953 0.3841958461773 -1.2645116977316
8 0.861137003272 1.075570148723 0.0678692859825 1.8855273485011
9 -0.818637090271 0.380513700320 0.7705661905513 0.5194362498010
10 -0.826471792680 -0.722518627125 -0.4871169940771 -0.0751340136879
# ... with 1 more variables: V5 <dbl>
data_frame / lst
データフレームおよびリストの作成
> a <- 1:5
> data_frame(a, b = a * 2)
# A tibble: 5 x 2
a b
<int> <dbl>
1 1 2
2 2 4
3 3 6
4 4 8
5 5 10
> data_frame(a, b = a * 2, c = 1)
# A tibble: 5 x 3
a b c
<int> <dbl> <dbl>
1 1 2 1
2 2 4 1
3 3 6 1
4 4 8 1
5 5 10 1
> data_frame(x = runif(10), y = x * 2)
# A tibble: 10 x 2
x y
<dbl> <dbl>
1 0.224379061256 0.448758122511
2 0.569050242659 1.138100485317
3 0.610360722756 1.220721445512
4 0.654660311993 1.309320623986
5 0.261591032147 0.523182064295
6 0.059085420100 0.118170840200
7 0.556450227043 1.112900454085
8 0.731231867801 1.462463735603
9 0.401210993761 0.802421987522
10 0.998451555613 1.996903111227
> lst(n = 5, x = runif(n))
$n
[1] 5
$x
[1] 0.3745450654533 0.0728190739173 0.5882274415344 0.6285342019983
[5] 0.7971250561532
frame_data
行方向の指定によるデータフレーム生成
> frame_data(
+ ~colA, ~colB,
+ "a", 1,
+ "b", 2,
+ "c", 3
+ )
# A tibble: 3 x 2
colA colB
<chr> <dbl>
1 a 1
2 b 2
3 c 3
> frame_data(
+ ~x, ~y,
+ "a", 1:3,
+ "b", 4:6
+ )
# A tibble: 2 x 2
x y
<chr> <list>
1 a <int [3]>
2 b <int [3]>
glimpse
データ構造を概観する
> glimpse(mtcars)
Observations: 32
Variables: 11
$ mpg <dbl> 21.0, 21.0, 22.8, 21.4, 18.7, 18.1, 14.3, 24.4, 22.8, 19....
$ cyl <dbl> 6, 6, 4, 6, 8, 6, 8, 4, 4, 6, 6, 8, 8, 8, 8, 8, 8, 4, 4, ...
$ disp <dbl> 160.0, 160.0, 108.0, 258.0, 360.0, 225.0, 360.0, 146.7, 1...
$ hp <dbl> 110, 110, 93, 110, 175, 105, 245, 62, 95, 123, 123, 180, ...
$ drat <dbl> 3.90, 3.90, 3.85, 3.08, 3.15, 2.76, 3.21, 3.69, 3.92, 3.9...
$ wt <dbl> 2.620, 2.875, 2.320, 3.215, 3.440, 3.460, 3.570, 3.190, 3...
$ qsec <dbl> 16.46, 17.02, 18.61, 19.44, 17.02, 20.22, 15.84, 20.00, 2...
$ vs <dbl> 0, 0, 1, 1, 0, 1, 0, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 1, 1, ...
$ am <dbl> 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, ...
$ gear <dbl> 4, 4, 4, 3, 3, 3, 3, 4, 4, 4, 4, 3, 3, 3, 3, 3, 3, 4, 4, ...
$ carb <dbl> 4, 4, 1, 1, 2, 1, 4, 2, 2, 4, 4, 3, 3, 3, 4, 4, 4, 1, 2, ...
repair_names
> list(3, 4, 5) %>% names()
NULL
> repair_names(list(3, 4, 5)) %>% names()
[1] "V1" "V2" "V3"
> mtcars %>% names()
[1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
[11] "carb"
> repair_names(mtcars) %>% names() # 変更を加えない
[1] "mpg" "cyl" "disp" "hp" "drat" "wt" "qsec" "vs" "am" "gear"
[11] "carb"
> tbl <- as_data_frame(structure(list(3, 4, 5), class = "data.frame"))
Error: Columns 1, 2, 3 must be named
> names(tbl)
NULL
> repair_names(tbl)
Error: `x` must be a vector
rownames / has_rownames / remove_rownames / rownames_to_column / column_to_rownames
行名に対する処理
> has_rownames(mtcars)
[1] TRUE
> has_rownames(iris)
[1] FALSE
> remove_rownames(mtcars) %>% has_rownames()
[1] FALSE
> mtcars_tbl <- as_data_frame(mtcars) %>% rownames_to_column()
> mtcars_tbl
# A tibble: 32 x 12
rowname mpg cyl disp hp drat wt qsec vs am
<chr> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 Mazda RX4 21.0 6 160.0 110 3.90 2.620 16.46 0 1
2 Mazda RX4 Wag 21.0 6 160.0 110 3.90 2.875 17.02 0 1
3 Datsun 710 22.8 4 108.0 93 3.85 2.320 18.61 1 1
4 Hornet 4 Drive 21.4 6 258.0 110 3.08 3.215 19.44 1 0
5 Hornet Sportabout 18.7 8 360.0 175 3.15 3.440 17.02 0 0
6 Valiant 18.1 6 225.0 105 2.76 3.460 20.22 1 0
7 Duster 360 14.3 8 360.0 245 3.21 3.570 15.84 0 0
8 Merc 240D 24.4 4 146.7 62 3.69 3.190 20.00 1 0
9 Merc 230 22.8 4 140.8 95 3.92 3.150 22.90 1 0
10 Merc 280 19.2 6 167.6 123 3.92 3.440 18.30 1 0
# ... with 22 more rows, and 2 more variables: gear <dbl>, carb <dbl>
> column_to_rownames(mtcars_tbl)
Warning: Setting row names on a tibble is deprecated.
# A tibble: 32 x 11
mpg cyl disp hp drat wt qsec vs am gear carb
* <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl> <dbl>
1 21.0 6 160.0 110 3.90 2.620 16.46 0 1 4 4
2 21.0 6 160.0 110 3.90 2.875 17.02 0 1 4 4
3 22.8 4 108.0 93 3.85 2.320 18.61 1 1 4 1
4 21.4 6 258.0 110 3.08 3.215 19.44 1 0 3 1
5 18.7 8 360.0 175 3.15 3.440 17.02 0 0 3 2
6 18.1 6 225.0 105 2.76 3.460 20.22 1 0 3 1
7 14.3 8 360.0 245 3.21 3.570 15.84 0 0 3 4
8 24.4 4 146.7 62 3.69 3.190 20.00 1 0 4 2
9 22.8 4 140.8 95 3.92 3.150 22.90 1 0 4 2
10 19.2 6 167.6 123 3.92 3.440 18.30 1 0 4 4
# ... with 22 more rows