将一个数据帧附加到 R 中另一个数据帧的末尾
在本文中,我们将使用 R 编程语言中的“$”运算符将数据帧附加到另一个数据帧。
方法
- 创建向量
- 通过传递这些向量创建一个数据帧 (dataframe1)
- 通过传递这些向量创建另一个数据帧 (dataframe2)
- 最后,使用“$”运算符将 dataframe2 附加到 dataframe1。
- 显示结果数据框
$运算符用于将数据框添加为列。
Syntax: dataframe_one$column_name=dataframe_two
Parameters:
- column_name is the name of the new column
- dataframe_two is the dataframe 2 that is appended
- dataframe_one is the first dataframe.
让我们将问题分解为更小的集群并使其理解,让我们首先独立创建两个数据帧。
R
# creating vectors for dataframe 1
names=c("bobby","sravan","ojaswi")
age=c(20,22,16)
# creating vectors for dataframe 2
address=c("kakumanu","kakumanu","hyderabad")
marks=c(89,98,90)
# pass these vectors to data frame1 a
a=data.frame(names,age)
# pass these vectors to data frame2 b
b=data.frame(address,marks)
print(a)
print(b)
R
# creating vectors for dataframe 1
names=c("bobby","sravan","ojaswi")
age=c(20,22,16)
# creating vectors for dataframe 2
address=c("kakumanu","kakumanu","hyderabad")
marks=c(89,98,90)
# pass these vectors to data frame1 a
a=data.frame(names,age)
# pass these vectors to data frame2 b
b=data.frame(address,marks)
print(a)
print(b)
print("-------'")
print("appending dataframe 2 to data frame1")
# appending using $ operator
a$other_details=b
print(a)
输出:
之后,创建了两个数据帧,让我们将一个附加到另一个中。
电阻
# creating vectors for dataframe 1
names=c("bobby","sravan","ojaswi")
age=c(20,22,16)
# creating vectors for dataframe 2
address=c("kakumanu","kakumanu","hyderabad")
marks=c(89,98,90)
# pass these vectors to data frame1 a
a=data.frame(names,age)
# pass these vectors to data frame2 b
b=data.frame(address,marks)
print(a)
print(b)
print("-------'")
print("appending dataframe 2 to data frame1")
# appending using $ operator
a$other_details=b
print(a)
输出: