a <- 'apple'
a
#> [1] "apple"
rm(list = ls())
a <- "apple"
b <- c(1,2,3)
c <- 1
ls()
#> [1] "a" "b" "c"
rm(a)
ls()
#> [1] "b" "c"
rm(list = ls())
You can use the environment panel in RStudio to browse variable in your environment.
## create txt file
# fileConn<-file("file.txt")
# writeLines(c("Hello","World"), fileConn)
# close(fileConn)
df <- c("Hello","World")
write.table(df, 'file2.txt')
df2 <- read.table('file2.txt')
print(df2)
df <- c("apple","graph")
write.csv(df, 'file3.csv')
df3 <- read.csv('file3.csv')
print(df3)
df <- c("apple3","graph3")
save(df, file = 'file4.Rdata')
load('file4.Rdata')
print(df)
Coverting between common data types in R. Can always go from a higher value i the table to a lower value
a <- c(TRUE, FALSE, TRUE)
print(a)
#> [1] TRUE FALSE TRUE
typeof(a)
#> [1] "logical"
a <- as.numeric(a)
print(a)
#> [1] 1 0 1
typeof(a)
#> [1] "double"
a <- as.logical(a)
print(a)
#> [1] TRUE FALSE TRUE
typeof(a)
#> [1] "logical"
a <- as.character(a)
print(a)
#> [1] "TRUE" "FALSE" "TRUE"
typeof(a)
#> [1] "character"
a <- as.factor(a)
print(a)
#> [1] TRUE FALSE TRUE
#> Levels: FALSE TRUE
typeof(a)
#> [1] "integer"
c(2, 4, 6) # Join elements into a vector
#> [1] 2 4 6
2:6 # An interger sequence
#> [1] 2 3 4 5 6
seq(2, 3, by=0.5) # A complex sequence
#> [1] 2.0 2.5 3.0
rep(1:2, times=3) # Repeat a vector
#> [1] 1 2 1 2 1 2
rep(1:2, each=3) # Repeat elements of a vector
#> [1] 1 1 1 2 2 2
x <- c(3,2,6,1,6,2)
sort(x)
#> [1] 1 2 2 3 6 6
rev(x)
#> [1] 2 6 1 6 2 3
table(x)
#> x
#> 1 2 3 6
#> 1 2 1 2
unique(x)
#> [1] 3 2 6 1
x <- c(3,2,6,1,6,2)
x[4]
#> [1] 1
x[-4]
#> [1] 3 2 6 6 2
x[2:4]
#> [1] 2 6 1
x[-(2:4)]
#> [1] 3 6 2
x[c(1,5)]
#> [1] 3 6
x <- c(3,2,6,1,6,2)
x[x==6]
#> [1] 6 6
x[x<3]
#> [1] 2 1 2
x[x %in% c(2,6)]
#> [1] 2 6 6 2
x['apple']
A list is a collection of elements which can be of different types.
# List 생성
l <- list(x = 1:5, y = c('a','b'))
l
#> $x
#> [1] 1 2 3 4 5
#>
#> $y
#> [1] "a" "b"
# List subset
l[[2]]
#> [1] "a" "b"
l[1]
#> $x
#> [1] 1 2 3 4 5
l$x
#> [1] 1 2 3 4 5
l['y']
#> $y
#> [1] "a" "b"
A special case of list where all elements are the same length.
# Data Frame 생성
df <- data.frame(x=1:3, y = c('a','b','c'))
df
# Understanding a data frame
# View(df)
head(df, 2)
nrow(df)
#> [1] 3
ncol(df)
#> [1] 2
dim(df)
#> [1] 3 2
A <- data.frame(x=1:3, y=c('a','b','c'))
B <- data.frame(y=11:13)
C <- data.frame(x=4:5, y=c('d','e'))
cbind(A, B)
rbind(A, C)
# paste
x = "Hello"
y = "World!"
z = "My name is DK"
paste(x, y)
#> [1] "Hello World!"
paste(x, y, sep='')
#> [1] "HelloWorld!"
paste(x, y, sep='-')
#> [1] "Hello-World!"
paste(z, collapse=' ')
#> [1] "My name is DK"
# Find regular expression matches in x.
pattern = "DK"
replace = "Dookyung"
grep(pattern, z)
#> [1] 1
# replace matches in x with a string.
gsub(pattern, replace, z)
#> [1] "My name is Dookyung"
toupper(replace)
#> [1] "DOOKYUNG"
tolower(replace)
#> [1] "dookyung"
nchar(replace)
#> [1] 8
# Factors
factor(x)
cut(x, breaks = 4)
for (variable in sequance){
Do something
}
for (i in 1:4){
j <- i + 10
print(j)
}
#> [1] 11
#> [1] 12
#> [1] 13
#> [1] 14
while (condition){
Do something
}
while (i < 5){
print(i)
i <- i + 1
}
#> [1] 4
if (condition){
Do something
} else {
Do something different
}
i = 5
if (i > 3){
print('Yes')
} else {
print('No')
}
#> [1] "Yes"
function_name <- function(var){
Do something
return(new_variable)
}
square <- function(x){
squared <- x * x
return(squared)
}
square(5)
#> [1] 25
a <- c(1, 4, NA, 6)
is.na(a)
#> [1] FALSE FALSE TRUE FALSE
is.null(a)
#> [1] FALSE
kind | Random_Var | Density_Func | Cumulative_Dist | Quantile |
---|---|---|---|---|
Normal | rnorm | dnorm | pnorm | qnorm |
Poisson | rpois | dpois | ppois | qpois |
Binomial | rbinom | dbinom | pbinom | qbinom |
Uniform | runif | dunif | punif | qunif |
x <- rnorm(30)
y <- rnorm(30) + 1:30
par(mfrow=c(1,2))
plot(x)
plot(y, x)
duration = faithful$eruptions
hist(duration, right=FALSE)