> #使用tibble
> library(tidyverse)
> a <- as_tibble(iris) #創(chuàng)建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可以使用剛剛創(chuàng)建的變量宪躯,而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可以用這些奇怪的符號(hào)嫂侍,R不認(rèn)識(shí),需要用反引號(hào)
+ `:)` = "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]] #第三行第一個(gè)
[1] 4.7
>
>
> #如何識(shí)別一個(gè)對(duì)象是否為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的區(qū)別
Error: object 'tibble和dataframe的區(qū)別' 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
>
>
>
> #建一個(gè)tibble
>
> annoying <- tibble(
+ `1` = 1:10,
+ `2` = `1` * 2 + rnorm(length(`1`))
+ )
> #畫(huà)個(gè)圖查看關(guān)系
> 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可以轉(zhuǎn)化vector為數(shù)據(jù)框
R 第七章tibble
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
- 文/潘曉璐 我一進(jìn)店門(mén)肠缔,熙熙樓的掌柜王于貴愁眉苦臉地迎上來(lái)夏跷,“玉大人,你說(shuō)我怎么就攤上這事明未〔刍” “怎么了?”我有些...
- 文/不壞的土叔 我叫張陵趟妥,是天一觀的道長(zhǎng)猫态。 經(jīng)常有香客問(wèn)我,道長(zhǎng)披摄,這世上最難降的妖魔是什么亲雪? 我笑而不...
- 正文 為了忘掉前任,我火速辦了婚禮疚膊,結(jié)果婚禮上义辕,老公的妹妹穿的比我還像新娘。我一直安慰自己寓盗,他們只是感情好灌砖,可當(dāng)我...
- 文/花漫 我一把揭開(kāi)白布璧函。 她就那樣靜靜地躺著,像睡著了一般基显。 火紅的嫁衣襯著肌膚如雪蘸吓。 梳的紋絲不亂的頭發(fā)上,一...
- 那天续镇,我揣著相機(jī)與錄音美澳,去河邊找鬼。 笑死摸航,一個(gè)胖子當(dāng)著我的面吹牛制跟,可吹牛的內(nèi)容都是我干的。 我是一名探鬼主播酱虎,決...
- 文/蒼蘭香墨 我猛地睜開(kāi)眼雨膨,長(zhǎng)吁一口氣:“原來(lái)是場(chǎng)噩夢(mèng)啊……” “哼!你這毒婦竟也來(lái)了读串?” 一聲冷哼從身側(cè)響起聊记,我...
- 序言:老撾萬(wàn)榮一對(duì)情侶失蹤,失蹤者是張志新(化名)和其女友劉穎恢暖,沒(méi)想到半個(gè)月后排监,有當(dāng)?shù)厝嗽跇?shù)林里發(fā)現(xiàn)了一具尸體,經(jīng)...
- 正文 獨(dú)居荒郊野嶺守林人離奇死亡杰捂,尸身上長(zhǎng)有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
- 正文 我和宋清朗相戀三年舆床,在試婚紗的時(shí)候發(fā)現(xiàn)自己被綠了。 大學(xué)時(shí)的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片嫁佳。...
- 正文 年R本政府宣布瓤漏,位于F島的核電站腾夯,受9級(jí)特大地震影響,放射性物質(zhì)發(fā)生泄漏蔬充。R本人自食惡果不足惜俯在,卻給世界環(huán)境...
- 文/蒙蒙 一、第九天 我趴在偏房一處隱蔽的房頂上張望娃惯。 院中可真熱鬧,春花似錦肥败、人聲如沸趾浅。這莊子的主人今日做“春日...
- 文/蒼蘭香墨 我抬頭看了看天上的太陽(yáng)皿哨。三九已至浅侨,卻和暖如春,著一層夾襖步出監(jiān)牢的瞬間证膨,已是汗流浹背如输。 一陣腳步聲響...
- 正文 我出身青樓,卻偏偏與公主長(zhǎng)得像崔步,于是被迫代替她去往敵國(guó)和親稳吮。 傳聞我的和親對(duì)象是個(gè)殘疾皇子,可洞房花燭夜當(dāng)晚...
推薦閱讀更多精彩內(nèi)容
- 知識(shí)點(diǎn) 卡方分布(Chi-square distribution) 四格表資料的卡方檢驗(yàn) 配對(duì)四格表的卡方檢驗(yàn) 四...
- ? 描述性統(tǒng)計(jì)分析? 頻數(shù)表和列聯(lián)表? 相關(guān)系數(shù)和協(xié)方差? t檢驗(yàn)? 非參數(shù)統(tǒng)計(jì) 7.1描述性...
- 如果還不知道時(shí)下最熱的《一出好戲》,那恐怕你要反省一下自己是不是與世隔絕太久了瑞你。這款來(lái)自黃渤的導(dǎo)演處女作酪惭,匯集了像...
- 早起有練云手和肩部運(yùn)動(dòng)。 中午到晴波園東邊的小公園里者甲,咱們上課的地方春感,樹(shù)綠得真好看,很春天的樣子过牙。 高高的楊樹(shù)下甥厦,...
- 家刀疙,在我們中國(guó)人的定義中,是一份責(zé)任的結(jié)合扫倡,是彼此真誠(chéng)相待谦秧、無(wú)私付出的地方,更是能夠白頭偕老撵溃、老少同樂(lè)的...