#在数据帧中加入行和列
#可以使用cbind()函数连接多个向量来创建数据帧。
#此外,可以使用rbind()函数合并两个数据帧。
city <- c("Tampa","Seattle","Hartford","Denver")
state <- c("FL","WA","CT","CO")
zipcode <- c(33602,98104,06161,80294)
addresses <- cbind(city,state,zipcode)
print("# # # # The First data frame
")
print(addresses)
new.address <- data.frame(
city = c("Lowry","Charlotte"),
state = c("CO","FL"),
zipcode = c("80230","33949"),
stringsAsFactors = FALSE
)
cat("# # # The Second data frame
")
print(new.address)
all.addresses <- rbind(addresses,new.address)
cat("# # # The combined data frame
")
print(all.addresses)
#用户自定义函数
# Create a function to print squares of numbers in sequence.
new.function <- function(a) {
for(i in 1:a) {
b <- i^2
print(b)
}
}
new.function(6)
#调用没有参数的函数
# Create a function without an argument.
new.function <- function() {
for(i in 1:5) {
print(i^2)
}
}
new.function()
#使用参数值调用函数(按位置和名称)
# Create a function with arguments.
new.function <- function(a,b,c) {
result <- a * b + c
print(result)
}
new.function(5,3,11)
new.function(a = 11, b = 5, c = 3)
#使用默认参数调用函数
# Create a function with arguments.
new.function <- function(a = 3, b = 6) {
result <- a * b
print(result)
}
new.function()
new.function(9,5)
#功能延迟计算
# Create a function with arguments.
new.function <- function(a, b) {
print(a^2)
print(a)
print(b)
}
# Evaluate the function without supplying one of the arguments.
new.function(6)