R包與R的初步數(shù)據(jù)操作
- R包下載與加載
-
R包的獲取
file.edit('~/.Rprofile') #生成R配置文件 options("repos" <- c(CRAN<-"https://mirrors.tuna.tsinghua.edu.cn/CRAN/")) # 配置CRAN鏡像 options(BioC_mirror <- "https://mirrors.ustc.edu.cn/bioc/") # 配置BiocManager鏡像- 加載R包
library('package') #需要事先install R包 require('package') #不需要事先install R包 -
R包下載與加載
test <- iris[c(1:2,51:52,101:102),]- mutate
mutate(.data <- test, new <- Sepal.Length * Sepal.Width) #生成新列new- select
var <- c('Sepal.Length', 'Sepal.Width') select(.data <- test, one_of(var)) #篩選列- filter
filter(test, Species %in% c("setosa","versicolor")) #篩選行- arrange
arrange(test, Sepal.Length) #按指定列排序(默認(rèn)升序) arrange(test, desc(Sepal.Length)) #降序排列- group_by
group_by(.data <- test, Species) #將test按照Species進(jìn)行分組- summarise
summarise(group_by(.data <- test, Species), mean(Sepal.Length), sd(Sepal.Length)) #將test分組后計(jì)算均數(shù)和標(biāo)準(zhǔn)差- %>%
test %>% group_by(Species) %>% summarise(mean(Sepal.Length), sd(Sepal.Length)) #管道傳參,將結(jié)果傳遞給下一個(gè)函數(shù)作為第一個(gè)參數(shù)- count
count(x <- test, Species) #計(jì)算非重復(fù)元素的個(gè)數(shù)- join
inner_join(test1, test2, by = 'x') #按x列共有元素合并 left_join(test1, test2, by = 'x') #按左側(cè)x列的元素合并 right_join(test1, test2, by = 'x') #按右側(cè)x列的元素合并 full_join(test1, test2, by = 'x') #按x列所有元素合并 semi_join(test1, test2, by = 'x') #返回左側(cè)x列在右側(cè)具有的元素 anti_join(test1, test2, by = 'x') #返回左側(cè)x列在右側(cè)不具有的元素- bind
bind_cols(test1, test3) #按列合并,需行數(shù)一致 bind_rows(test1, test2) #按行合并,需列數(shù)一致
-