R 第七章tibble
2018-10-29 本文已影响0人
liyin_d64b
> #使用tibble
> library(tidyverse)
> a <- as_tibble(iris) #创建tibble
> tibble(
+ x=1:5,
+ y=1,
+ z=x^2+y
+ )
# A tibble: 5 x 3
x y z
<int> <dbl> <dbl>
1 1 1 2
2 2 1 5
3 3 1 10
4 4 1 17
5 5 1 26
>
> x=1:5
> y=1
> data.frame( #tibble可以使用刚刚创建的变量,而dataframe则需要先定义
+ x=1:5,
+ y=1,z=x+y
+ )
x y z
1 1 1 2
2 2 1 3
3 3 1 4
4 4 1 5
5 5 1 6
>
>
> tb <- tibble( #tibble可以用这些奇怪的符号,R不认识,需要用反引号
+ `:)` = "smile",
+ ` ` = "space",
+ `2000` = "number"
+ )
> tb
# A tibble: 1 x 3
`:)` ` ` `2000`
<chr> <chr> <chr>
1 smile space number
>
>
> tribble( #tribble
+ ~x, ~y, ~z,
+ #--|--|----
+ "a", 2, 3.6,
+ "b", 1, 8.5
+ )
# A tibble: 2 x 3
x y z
<chr> <dbl> <dbl>
1 a 2 3.6
2 b 1 8.5
>
>
> data <- iris
> data[[3,1]] #第三行第一个
[1] 4.7
>
>
> #如何识别一个对象是否为tibble
> #is_tibble(),或者class()
> class(data)
[1] "data.frame"
> class(a)
[1] "tbl_df" "tbl" "data.frame"
> is_tibble(a)
[1] TRUE
> is_tibble(data)
[1] FALSE
>
> #Tibbles has the classes c("tbl_df", "tbl", "data.frame")
>
> tibble和dataframe的区别
Error: object 'tibble和dataframe的区别' not found
>
> #Using $ a data.frame will partially complete the column.
> #So even though we wrote df$x it returned df$xyz.
> #This saves a few keystrokes, but can result in accidentally using a different variable than you thought you were using.
>
> #With data.frames, with [ the type of object that is returned differs on the number of columns.
> #If it is one column, it won’t return a data.frame, but instead will return a vector.
> #With more than one column, then it will return a data.frame.
> #This is fine if you know what you are passing in, but suppose you did df[ , vars] where vars was a variable.
> #Then you what that code does depends on length(vars) and you’d have to write code to account for those situations or risk bugs.
>
>
> var <- "Species" #如果你把变量保存在var里面,需要用[[]]提取,而不是$
> data[[var]]
[1] setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa
[11] setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa
[21] setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa
[31] setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa
[41] setosa setosa setosa setosa setosa setosa setosa setosa setosa setosa
[51] versicolor versicolor versicolor versicolor versicolor versicolor versicolor versicolor versicolor versicolor
[61] versicolor versicolor versicolor versicolor versicolor versicolor versicolor versicolor versicolor versicolor
[71] versicolor versicolor versicolor versicolor versicolor versicolor versicolor versicolor versicolor versicolor
[81] versicolor versicolor versicolor versicolor versicolor versicolor versicolor versicolor versicolor versicolor
[91] versicolor versicolor versicolor versicolor versicolor versicolor versicolor versicolor versicolor versicolor
[101] virginica virginica virginica virginica virginica virginica virginica virginica virginica virginica
[111] virginica virginica virginica virginica virginica virginica virginica virginica virginica virginica
[121] virginica virginica virginica virginica virginica virginica virginica virginica virginica virginica
[131] virginica virginica virginica virginica virginica virginica virginica virginica virginica virginica
[141] virginica virginica virginica virginica virginica virginica virginica virginica virginica virginica
Levels: setosa versicolor virginica
>
>
>
> #建一个tibble
>
> annoying <- tibble(
+ `1` = 1:10,
+ `2` = `1` * 2 + rnorm(length(`1`))
+ )
> #画个图查看关系
> ggplot(annoying, aes(x = `1`, y = `2`)) +
+ geom_point()
>
> #增加一列
> annoying[["var"]] <- annoying$`2` / annoying$`1`
>
> annoying
# A tibble: 10 x 3
`1` `2` var
<int> <dbl> <dbl>
1 1 2.90 2.90
2 2 4.72 2.36
3 3 4.88 1.63
4 4 8.71 2.18
5 5 9.23 1.85
6 6 10.9 1.81
7 7 13.6 1.94
8 8 14.6 1.83
9 9 18.7 2.07
10 10 21.6 2.16
> #重命名
> annoying <- rename(annoying, one = `1`, two = `2`, three = "var")
> glimpse(annoying)
Observations: 10
Variables: 3
$ one <int> 1, 2, 3, 4, 5, 6, 7, 8, 9, 10
$ two <dbl> 2.895630, 4.716864, 4.875581, 8.707593, 9.232054, 10.872596, 13.613199, 14.638820, 18.674116, 21.584095
$ three <dbl> 2.895630, 2.358432, 1.625194, 2.176898, 1.846411, 1.812099, 1.944743, 1.829853, 2.074902, 2.158410
>
>
>
> enframe(c(a = 1, b = 2, c = 3))
# A tibble: 3 x 2
name value
<chr> <dbl>
1 a 1
2 b 2
3 c 3
> #enframe可以转化vector为数据框