熱線電話:13121318867

登錄
首頁精彩閱讀把數據輸入R語言中,如何進行簡單的操作(一)
把數據輸入R語言中,如何進行簡單的操作(一)
2020-09-04
收藏

作者:丁點helper

來源:丁點幫你

回憶一下上一講用到的例子:

輸入數據的代碼在上一講詳細講解過,這里總結如下:

age <- c(25, 34, 59, 60, 20)   
#患者年齡type <- c(1, 2, 2, 2, 1)       
#糖尿病類型status <- c("poor", "improved", "excellent", "poor", "excellent")  
#病情comorbidity<- c(TRUE, FALSE, FALSE, TRUE, FALSE)   
#出現并發癥

age、type、status、comorbidity中分別僅有一種數據類型,它們都是向量。本文介紹生成向量之后,如何對其進行簡單的操作。

1. 查看與改變向量的數據類型
看到一個向量,首先要搞清楚其中包含的數據類型。就本例而言,從表面看也很容易區分,但實際上一項統計分析工作用到的向量可能很多,用函數class()可以快速知曉某向量的數據類型,例如:

class(age) [1]
"numeric"class(type) [1] 
"numeric"class(status) [1]
"character"class(comorbidity)[1]
"logical"

不同類型的數據可以根據需要互相轉換,用as.目標數據類型()函數:

as.numeric() 
#將括號中的內容轉變為數值型數據
as.character() 
#轉變為字符型as.logical()  
#轉變為邏輯型as.factor()    
#轉變為因子型

所以也可用as.character()將type中的數值型數據轉變為字符型:

type[1] 
1 2 2 2 1class(type) [1] 
"numeric"
type <- as.character(type)   
# 注意要將新的結果賦值給typetype[1] "1" "2" "2" "2" "1"class(type)[1] "character"

之前講過,將定性變量(即分類變量)以因子的形式輸入會有助于后續的統計分析工作,factor()這個函數可以幫我們把數據轉變為因子型:

type <- c(1, 2, 2, 2, 1) 
type <- factor(type)
type[1] 
1 2 2 2 1
Levels: 1 2class(type)[1]
"factor"

用1和2這樣的阿拉伯數字其實不太利于準確地表達數據內容,可以給原來的1和2加上標簽:

type <- factor(type, levels = c("1", "2"), 
               labels = c("Type 1", "Type 2")) 
type[1] 
Type 1 Type 2 Type 2 Type 2 Type 1
Levels: Type 1 Type 2

所以在輸入定性變量(分類變量)時可以采用這種簡便方法。

再看另一個例子:

status[1]
"poor"
"improved" 
"excellent"
"poor" 
"excellent"
status <- factor(status)status[1] 
poor  improved  excellent poor  excellentLevels: 
excellent improved poorclass(status)[1]
"factor"

由于status是一個有序分類變量,所以在轉變為因子時還應體現其順序:

status <- factor(status, levels = c('poor', 'improved','excellent'),ordered = TRUE) 
status[1] 
poor  improved  excellent 
poor  excellentLevels:
poor < improved < excellent
這里的順序是根據levels這個命令中的內容生成的,可自行調整levels命令中的順序:
status <- factor(status, levels = c('excellent','improved' ,'poor'),ordered = TRUE)
status[1]
poor improved  excellent 
poor  excellentLevels: 
excellent < improved < poor

2. 向量中的數據定位

以age這個向量為例:

age <- c(25, 34, 59, 60, 20) 
age
[1] 25 34 59 60 20

輸出向量中排在第3位的數據:

age[3]
[1] 59

輸出排在1,2,5位的數據:

age[c(1,2,5)]
[1] 25 34 20

輸出1至3位的數據:

age[c(1:3)]  
[1] 25 34 59

3. 向量中的數據計算

以age這個向量為例:

age <- c(25, 34, 59, 60, 20)
# 仍以age為例age
[1] 25 34 59 60 20
age+4       
# 給向量中每個數都加4
[1] 29 38 63 64 24
sqrt(age)   
# 求平方根
[1] 5.000000 5.830952 7.681146 7.745967 4.472136
sort(age)   
# 給數據從低到高排序
[1] 20 25 34 59 60
sort(age, decreasing =T)    
# 給數據從高到低排序
[1] 60 59 34 25 20
age2 <- c(20,30,40,50,60)   
# 再生成一個向量  
age+age2    
# 將兩向量中的元素相加
[1]  45  64  99 110  80

4. 生成特定形式的向量

生成重復數據。用rep(x, ……),x表示要重復的內容。

rep(1,times=5)   
#times表示重復的次數
[1] 1 1 1 1 1
rep(c(1,2),4)   
#times這個表達可以省略
[1] 1 2 1 2 1 2 1 2
rep(c(1,2),each=4)   
#each也是針對重復次數的命令
[1] 1 1 1 1 2 2 2 2

特定間隔的數據。用seq(from,to,by)這個函數,from為起始值,to為終止值,by為數據之間的間隔。

seq(1,100,19)  
#from,to,by都可以省略 
[1]  1 20 39 58 77 96
seq(1,10)   
#如果不指定by的內容,則默認為1
[1]  1  2  3  4  5  6  7  8  9 10

下一篇介紹數據框的相關操作。

數據分析咨詢請掃描二維碼

若不方便掃碼,搜微信號:CDAshujufenxi

數據分析師資訊
更多

OK
客服在線
立即咨詢
日韩人妻系列无码专区视频,先锋高清无码,无码免费视欧非,国精产品一区一区三区无码
客服在線
立即咨詢