📜  如何在 R 中组合两个向量?

📅  最后修改于: 2022-05-13 01:55:34.115000             🧑  作者: Mango

如何在 R 中组合两个向量?

在本文中,我们将学习如何结合两个 R 编程语言中的向量。我们可以使用函数c()本身组合两个或多个向量。在使用函数c() 时,所有参数都被强制转换为一个通用类型,即返回值的类型。

脚步 -

  • 创建要组合的向量
  • 使用 c() 组合它们
  • 显示组合结果

示例 1:相同数据类型的向量将给出输入数据类型的向量作为结果。

R
a <- c(1, 2, 8) 
b <- c(5, 8, 9, 10) 
c <- c(a,b)
c
  
cat("typeof a", typeof(a), " typeof b", typeof(b),
    "typeof c",typeof(c) , "\n")
  
a <- c("geek","for","geek")
b <- c("hello","coder")
c <- c(a,b)
c
  
cat("typeof a", typeof(a), " typeof b", typeof(b),
    "typeof c",typeof(c) , "\n")
  
a <- c(TRUE, FALSE, NA)
b <- c(TRUE, FALSE)
c <- c(a,b)
c
  
cat("typeof a", typeof(a), " typeof b", typeof(b),
    "typeof c",typeof(c) , "\n")


R
a <- c(1, 2, 8) 
b <- c("hello","coder")
c <- c(a,b)
  
# a vector of type double and b vector of type 
# character result c vector of type character
c
  
cat("typeof a", typeof(a), " typeof b", typeof(b), 
    "typeof c",typeof(c) , "\n")
  
a <- c("geek","for","geek")
b <- c(TRUE, FALSE)
  
# a vector of type character and b vector of type logical
# result c vector of type character
c <- c(a,b)
c
  
cat("typeof a", typeof(a), " typeof b", typeof(b), 
    "typeof c",typeof(c) , "\n")
  
a <- c(1, 2, 8) 
b <- c(TRUE, FALSE)
c <- c(a,b)
  
# a vector of type double and b vector of type 
# logical result c vector of type double
c
  
cat("typeof a", typeof(a), " typeof b", typeof(b),
    "typeof c",typeof(c) , "\n")


输出:



组合 double 和字符类型的向量时, 字符和 logical , double 和 logical c()函数返回字符、 字符、 double 类型的向量。

示例 2:



电阻

a <- c(1, 2, 8) 
b <- c("hello","coder")
c <- c(a,b)
  
# a vector of type double and b vector of type 
# character result c vector of type character
c
  
cat("typeof a", typeof(a), " typeof b", typeof(b), 
    "typeof c",typeof(c) , "\n")
  
a <- c("geek","for","geek")
b <- c(TRUE, FALSE)
  
# a vector of type character and b vector of type logical
# result c vector of type character
c <- c(a,b)
c
  
cat("typeof a", typeof(a), " typeof b", typeof(b), 
    "typeof c",typeof(c) , "\n")
  
a <- c(1, 2, 8) 
b <- c(TRUE, FALSE)
c <- c(a,b)
  
# a vector of type double and b vector of type 
# logical result c vector of type double
c
  
cat("typeof a", typeof(a), " typeof b", typeof(b),
    "typeof c",typeof(c) , "\n")

输出: