随笔分类 - R语言
摘要:1、 测试数据 > df <- data.frame(Age=c(22,24,25,26), Gender=c("Girl","Girl","Boy","Boy"), + City=c("Shanghai","Beijing","Guangzhou","Nanjing")) > df Age Gen
阅读全文
摘要:1、测试数据 > dat <- as.data.frame(matrix(1:100, nrow = 10)) > colnames(dat) <- 1:10 > rownames(dat) <- 1:10 > dat 1 2 3 4 5 6 7 8 9 10 1 1 11 21 31 41 51
阅读全文
摘要:1、方法1 模拟200个样本, 50000个位点 nsnp <- 50000 nind <- 200 nums <- sample(1:2, nsnp * 2 * nind, replace = T) snp_matrix <- matrix(nums, nrow = 200) col_idx <-
阅读全文
If libcurl is already installed, check that 'pkg-config' is in your PATH and PKG_CONFIG_PATH contain
摘要:1、R安装devtools过程中遇到如下问题: > install.packages('devtools', repos='https://mirror.lzu.edu.cn/CRAN/') 2、解决方法 sudo apt-get install libssl-dev sudo apt-get in
阅读全文
摘要:1、 > a <- letters[1:10] > a [1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" > replace(a, c(1, 3, 5), "xxx") ## 替换指定索引的字符串为特定的字符串 [1] "xxx" "b" "xxx" &qu
阅读全文
摘要:1、 root@DESKTOP-1N42TVH:/home/test2# ls a.txt test.r root@DESKTOP-1N42TVH:/home/test2# cat test.r #!/usr/bin/Rscript library(optparse) option_list <-
阅读全文
摘要:1、 root@DESKTOP-1N42TVH:/home/test2# ls test.r root@DESKTOP-1N42TVH:/home/test2# cat test.r ## 测试函数 argv <- commandArgs(TRUE) ## 参数收集在argv数组中 print(pa
阅读全文
摘要:1、自定义函数之位置参数 > testfun1 <- function(arg1, arg2){ ## 自定义函数,给与两个参数arg1、arg2 + result = arg1 - arg2 + print(result) + } > testfun1(10, 3) ## 默认就是位置参数,即ar
阅读全文
摘要:001、 > a <- c("abc", "bcd", "ead") > b <- c("ead", "aya", "dbe") > c <- c("mna", "ond", "dne") > d <- data.frame(a, b, c) > d a b c 1 abc ead mna 2 bc
阅读全文
摘要:1、 > sub("b", "x", c("abbc", "bbcd", "cde")) ## 三个字符串中第一个“b”被替换为了“x” [1] "axbc" "xbcd" "cde" > gsub("b", "x", c("abbc", "bbcd", "cde")) ##gsub中的g表示全局,
阅读全文
摘要:1、数据框或矩阵中字符串的批量替换 > a <- c("a","d", "v", "c") > b <- c("c", "b", "a", "d") > c <- c("m", "a", "d", "b") > d <- data.frame(a, b, c) > d a b c 1 a c m 2
阅读全文
摘要:001、 > a <- c(5, 2, 7, 4) > b <- c(3, 8, 6, 5) > c <- c(7, 1, 6, 3) > dat <- list(a, b, c) ## 生成测试列表 > dat [[1]] [1] 5 2 7 4 [[2]] [1] 3 8 6 5 [[3]] [
阅读全文
摘要:001、 > a <- 1:3 > b <- "xxx" > c <- c("bbb", "aaa", "ccc") > dat <- list(a, b, c) ## list函数用于生成列表 > dat [[1]] [1] 1 2 3 [[2]] [1] "xxx" [[3]] [1] "bbb
阅读全文
摘要:001、 > c1 <- c(8, 2, 9, 3) > c2 <- c(6, 3, 7, 4) > c3 <- c(3, 1, 7, 6) > dat <- cbind(c1, c2, c3) > dat ## 测试数据, 为数据框 c1 c2 c3 [1,] 8 6 3 [2,] 2 3 1 [
阅读全文
摘要:001、 > dat1 <- c(3, 4, 1, 5, 7, 2) > dat1 <- c(3, 4, 1, 5, 7, 2) > dat2 <- c(6, 3, 2, 7, 4, 9) > dat1 %in% dat2 ## %in% 用于返回dat1中的每一个元素是否在dat2中,如果是返回T
阅读全文
摘要:001、 > dat <- c(3, 7, 9, 1, 2, 3, 4, 4, 7, 5) > dat [1] 3 7 9 1 2 3 4 4 7 5 > which.max(dat) ## 返回最大值的索引 [1] 3 > which.min(dat) ## 返回最小值的索引 [1] 4 > wh
阅读全文
摘要:1、 > dat <- rep(4, 4) > dat [1] 4 4 4 4 > res1 <- unique(dat) > res1 [1] 4 > res2 <- rep(4,4) %>% unique() ## R语言中%>%表示管道符,即把前面数输出作为后面的输入 > res2 [1] 4
阅读全文
摘要:001、 > signif(0.03253, 3) ## 三位有效数字 [1] 0.0325 > signif(0.03253, 1) [1] 0.03
阅读全文
摘要:001、数值 > a = 3.336345 > round(a) [1] 3 > round(a, digits = 2) [1] 3.34 > round(a, digits = 3) [1] 3.336 002、适用于其他数据类型(向量、数据框;不适用于矩阵) > a <- c(3.345, 5
阅读全文
摘要:001、取余数 > 5 %% 2 ## 取余数 [1] 1 > 10 %% 3 ## 取余数 [1] 1 002、除法 > 5 / 2 ## 除法 [1] 2.5 > 10 / 3 [1] 3.333333 003、地板除法 > 5 %/% 2 ## 地板除法 [1] 2 > 10 %/% 3 [1
阅读全文

浙公网安备 33010602011771号