學(xué)習(xí)小組Day6筆記--安之若素

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ù)一致
      
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容