R语言 改变数据帧 和 函数的用法 几段代码

改变数据帧 和 函数的用法 几段代码


#在数据帧中加入行和列
#可以使用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)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值