Recently Published
Publish Document
1:10
5:-5
seq(from=23, by=2, length=12)
seq(from=10, by=10, length=10)
seq(from=10, to=100, by=10)
rep(2,10)
rep(c(4,5), each=3)
# Ten random numbers between 0 and 1
runif(10)
# Five random numbers between 100 and 1000
runif(5, 100, 1000)
rnorm(10, 100, 10)
plot(rnorm(10,100,10))
numbers <- 1:15
sample(numbers, size=15, replace=TRUE)
sample(numbers, size=15, replace=FALSE)
write.csv(mtcars, "Test-mtcars.csv")
read.csv("Test-mtcars.csv")
allom <- read.csv("Test-mtcars.csv", header=TRUE)
head(allom)
head(allom, n=5)
tail(allom, n=5)
rownames(allom)
colnames(allom)
write.table(mtcars, "Test-mtcars1.txt")
read.table("Test-mtcars1.txt")
allom1 <- read.table("Test-mtcars1.txt")
head(allom1)
head(allom1, n=5)
tail(allom1, n=5)
rownames(allom1)
colnames(allom1)
allom1$model <- rownames(allom1)
rownames(allom1) <- NULL
library(stringr)
allom1$maker <- word(allom1$model, 1)
allom1
read.table(header=TRUE, text="
a b
1 2
3 4
")
# convert vectros into a dataframe
vec1 = c(9,10,1,2,45)
vec2 = 1:5
data.frame(x=vec1, y=vec2) # x=y=numbers
# data.table(x=vec1, y=vec2) # Error
# data.matrix(x=vec1, y=vec) # Error
vec3 = c(9,10,1,2,45)
vec4 = 1:10
data.frame(x=vec3, y=vec4) # x numbers ars smller than y
# vec5 = c(9,10,1,2,45)
# vec6 = 1:3
# data.frame(x=vec5, y=vec6) # Error y is smaller than x
head(allom1)
str(allom1)
round(allom1$mpg, digits=1)
round(allom1$mpg, digits=-1)
allom1$new_mpg <- with(allom1, mpg^2)
allom1$new_mpg1 <- allom1$mpg^2
head(allom1)
allom1$vs <- NULL
head(allom1)
summary(allom1)
nrow(allom1)
ncol(allom1)
nums1 <- c(1,4,2,8,11,100,8)
nums2 <- c(3.3,8.1,2.5,9.8,21.2,13.8,0.9)
nums1[2]
nums1[nelements<-length(nums1)]
nelements
nums1[1:3]
nums1[c(1,7,6)]
nums1[seq(1,7,by=2)]
1:length(nums2)
nums2[sample(1:length(nums2), 5)]
length(nums2)
nums1[-1]
nums1[-c(1, length(nums1))]
#logical
nums2[nums2>10]
nums2[nums2 > 5 & nums2 < 10]
nums2[nums2 < 1 | nums2 > 20]
nums1[nums1 == 8] # exactly
nums1[nums1 != 100] # NOT equal
# Subset of nums1, where value is one of 1,4 or 11:
# %in% "is an element of"
nums1[nums1 %in% c(1,4,11)]
# Subset of nums1, where value is NOT 1,4 or 11:
nums1[!(nums1 %in% c(1,4,11))]
# Where nums1 was 100, make it -100
nums1[nums1 == 100] <- -100
nums1
# Where nums2 was less than 5, make it zero
nums2[nums2 < 5] <- 0
nums2