r 最大R分钟

#R#min_max1

foundational_programming-r-min_max.R
r_min <- function(x) {
  out <- x[[1]]
  for (i in x[-1]) {
    if (i < out) {
      out <- i
    }
  }
  out
}

r_max <- function(x) {
  out <- x[[1]]
  for (i in x[-1]) {
    if (i > out) {
      out <- i
    }
  }
  out
}

r_vector <- 1:20
r_vector
#> [1]  1  2  3  4  5  6  7  8  9 10 11 12 13 14 15 16 17 18 19 20

r_min(r_vector)
#> 1

r_max(r_vector)
#> 20

r R独特的套装

R独特的套装

foundational_programming-r-unique2.R
r_vector <- c(1, 2, 3, 1, 2, 3)

unique(r_vector)
#> [1] 1 2 3

r R唯一的值集

R唯一的值集

foundational_programming-r-unique1.R
r_set <- function(x) {
  out <- vector(mode = mode(x))
  for (i in x) {
    if(!i %in% out) {
      out <- c(out, i)
    }
  }
  out
}

r_vector <- c(1, 2, 3, 1, 2, 3)

r_set(r_vector)
#> [1] 1 2 3

r apply - list not first arg of function

当存在您不希望再次输入的预设函数参数时,该列表不是该函数的第一个参数

apply_not_first_arg.R

myfun <- function(pronoun, verb, y = "swimming"){
  return(paste(pronoun, verb, y))  
}

sapply(c("go", "went", "like"), function(iter) myfun("I", iter))

r R:内置长度

R:内置长度

foundational_programming-built_in-length.R
r_vector <- 1:20
length(r_vector)
#> [1] 20

r R:从头开始的长度

R:从头开始的长度

foundational_programming-from_scratch-r_length.R
r_length <- function(x) {
  out <- 0L
  for (i in x) {
    out <- out + 1L
  }
  out
}

r_vector <- 1:20
r_length(r_vector) 
#> [1] 20

r t_test

做依赖和独立测试

t_test
library(stats)
library(lsr)

# load data
pre = read.csv("pre_survey.csv")
post = read.csv("post_survey.csv")



# compare variance 
var(pre$pre_score)
var(post$post_score)

# conduct t tests for difference in score between surveys
t.test(pre$pre_score, post$post_score, var.equal = TRUE)
# p value is 0.421
# no difference between mean scores

# examine differences between those who saw campaign or not
post_saw = subset(post, exposure == "Yes")
post_didntsee = subset(post, exposure == "No")

t.test(post_saw$post_score, post_didntsee$post_score, var.equal = TRUE)

r 将(多个)列作为变量ggplot2传递

以编程方式为ggplot2提供变量(适用于v 3.0.0)

column_as_var_ggplot2.R
# for a single column

require(tidyverse)
plot_iris <- function(my_y_col) {
  my_y_col <- enquo(my_y_col) # intercept and pass on the expression rather than evaluating
  ggplot(iris, aes(x = Species, y = !!my_y_col)) + geom_boxplot() # !! unpackage what was enquo'd
}
plot_iris(Petal.Length)

# for multiple columns:

my_function <- function(filter_col, filter_val) {
  df <- diamonds
  for (n in 1:length(filter_col)) {
    df <- df %>%
      filter(!!filter_col[[n]] == filter_val[[n]])
  }
  return(df)
}
my_function(c(quo(cut), quo(color)), c("Good", "E"))

r 列出对象的内存大小[R]

列出对象的内存大小[R]

memorySize.R
## --------------------------------------------------
## --------------------------------------------------
##
## List memory size of objects [R]
## Muffins 'n' Code
## https://github.com/jorgeassis
##
## --------------------------------------------------
## --------------------------------------------------

list.memory <- function (pos = 1, pattern, order.by = "Size", decreasing=TRUE, head = TRUE, n = 10) {
  
  napply <- function(names, fn) sapply(names, function(x)
    fn(get(x, pos = pos)))
  names <- ls(pos = pos, pattern = pattern)
  obj.class <- napply(names, function(x) as.character(class(x))[1])
  obj.mode <- napply(names, mode)
  obj.type <- ifelse(is.na(obj.class), obj.mode, obj.class)
  obj.size <- napply(names, object.size) / 10^6 # megabytes
  obj.dim <- t(napply(names, function(x)
    as.numeric(dim(x))[1:2]))
  vec <- is.na(obj.dim)[, 1] & (obj.type != "function")
  obj.dim[vec, 1] <- napply(names, length)[vec]
  out <- data.frame(obj.type, obj.size, obj.dim)
  names(out) <- c("Type", "Size", "Rows", "Columns")
  out <- out[order(out[[order.by]], decreasing=decreasing), ]
  if (head)
    out <- head(out, n)
  out
  
}

r 线性回归线abline r

r
plot(decathlon$X100m,decathlon$Longueur,xlab="100m",ylab="Longueur",pch=18,col=
"blue")
abline(lm(decathlon$Longueur~decathlon$X100m),col="red")
cor(decathlon$X100m,decathlon$Longueur)
text(11.2,7.95,paste("Corr=",cor(decathlon$X100m,decathlon$Longueur)),cex=2)