随笔分类 - R语言
摘要:001、 a <- c(2, 3, 6) b <- c(3, 1, 4) temp = 0 ## 利用循环实现 for (i in 1:length(a)) { temp = temp + a[i] * b[i] } temp 2、内置函数实现 a <- c(2, 3, 6) b <- c(3, 1
阅读全文
摘要:001、 dat <- data.frame(matrix(, nrow = 5, ncol = 0)) ## 创建空数据框, 指定行数为5 dat a = 1:5 cbind(dat, a) ## 可以实现直接合并新的列
阅读全文
摘要:001、 一下查询结果显示没有安装。 pkgbuild::find_rtools(debug = TRUE) 002、安装rtools(以版本对应) 下载rtools:https://cran.r-project.org/bin/windows/Rtools/ 003、双击下载好的安装包进行安装,
阅读全文
摘要:001、 library(dplyr) dat <- read.table("xx.map", header = F) ## 测试数据 dat dat %>% group_by(V1) %>% top_n(n = 2, wt = V2) ## 以第一列分类, 然后按照第二列从大到小排序,并提取前两个
阅读全文
摘要:001、 test <- as.factor(c(2, 3, 7, 8)) test as.numeric(test) ## 不能直接将因子型转换为数值型 as.numeric(as.character(test)) ## 先转换为字符,然后转换为数值
阅读全文
摘要:001、 dat3 <- matrix(1:16, nrow = 4, byrow = T) dat3 min(dat3) mean(dat3) max(dat3) dat3[] <- 0 ## 将矩阵元素全部设置为0(可以为任意元素) dat3
阅读全文
摘要:001、 data <- data.frame(x1 = c(1, 2, 3, 4), x2 = c(5, 6, 7, 8)) ## 测试数据框 data transform(data, x1 = x1 + 10) ## 修改列 transform(data, x1 = x1 + 10, x2 =
阅读全文
摘要:1、在任意列之间插入新数据 a <- 1:5 b <- letters[1:5] c <- LETTERS[1:5] d <- 5:1 dat <- data.frame(a, b, c, d) dat x <- rep("k", 5) cbind(dat[,1:2], x, dat[,3:4])
阅读全文
摘要:001、 c1 <- paste0("snp", sample(1:20, 6)) c1 sort(c1) c1[order(c1)] library(gtools) ## 使用gtools包中的函数 mixedsort(c1) c1[mixedorder(c1)]
阅读全文
摘要:001、 dir() dat <- read.table("outcome.ped") temp <- dat temp temp$id1 <- "A" temp$id2 <- "C" temp$id3 <- "G" temp$id4 <- "T" temp <- t(temp) result <-
阅读全文
摘要:001、 a <- 1:3 b <- letters[1:3] c <- LETTERS[1:3] d <- rep("K",3) vx <- paste(a, b, c, sep = "_") vx dat <- data.frame(vx, d) ## 测试数据框 dat library(tid
阅读全文
摘要:001、 a <- 1:3 b <- letters[1:3] c <- LETTERS[1:3] d <- rep("K",3) dat <- data.frame(a, b, c, d) ## 生成测试数据框 dat library(tidyr) dat1 <- unite(dat, "cxx"
阅读全文
摘要:001、 root@PC1:/home/test2# cat test.txt 1_55910>snp1 0.05591 1_85204>snp2 0.085204 1_122948>snp3 0.122948 1_203750>snp4 0.20375 1_312707>snp5 0.312707
阅读全文
摘要:str_detect函数属于tidyverse包中函数, 功能类似于grepl函数。 001、 str1 <- c("xx", "yy", "zz", "xx", "pp", "xx") str1 grepl("xx", str1) str_detect("xx", str1)
阅读全文
摘要:001、 library(cowplot) library(ggplot2) a <- ggplot(mtcars, aes(x = mpg)) + geom_histogram(binwidth = 4) + theme(plot.margin=unit(c(1, 1, 1, 1),'cm'))
阅读全文
摘要:例:exp(x): R语言中exp函数用于返回e的x次方的值。 001、 exp(4) 2.718281828459**4 exp(0) 2.718281828459**0 exp(-5) 2.718281828459**-5 dat <- c(-7, 0, 7) exp(dat) 2.718281
阅读全文
摘要:001、 r1 <- c("CC","CC","GG","TT") r2 <- c("GG","GG","GC","TT") r3 <- c("CC","GG","GG","TT") dat <- data.frame(r1, r2, r3) ## 测试数据框 dat gsub(pattern =
阅读全文
摘要:001、 library(testthat) ## 函数属于testthat包 expect_true( 2 == 2) ## 在程序中用于判断条件是否成立 expect_true( 2 != 2)
阅读全文
摘要:001、读取测试数据 dir() dat <- read.table("test.txt") dat 002、按照第一列进行升序排列 dat dat[order(dat[,1]),] 03、第一例按照降序进行排序 dat[order(-dat[,1]),] 004、第一列升序、第二列降序 dat[o
阅读全文
摘要:001、测试数据 library(dplyr) class1 <- tribble( ~'名次',~'姓名', '第一名','王某人', '第二名','张周人', '第三名','李某人' ) class2 <- tribble( ~'名次',~'姓名', '第一名','胡某人', '第二名','刘周
阅读全文

浙公网安备 33010602011771号