📜  如何向 R 数据框添加一行?

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

如何向 R 数据框添加一行?

在本文中,我们将看到如何在 R 编程语言中向 DataFrame 添加行。为此,我们将使用 rbind()函数。 R语言中的该函数用于按行组合指定的Vector、Matrix或Data Frame。

句法:

示例 1:

R
# creating a data frame with some data
df9 = data.frame(id=c(1,2,3),
                 name=c("karthik","bhagiradh","kethan")) 
  
print("Original data frame")
  
# printing the data frame 
print(df9) 
  
# declaring a row of values in 
# data.frame() function
df8 = data.frame(4,"shyam") 
  
# adding names to the row values
names(df8)=c("id","name")
  
# passing the original data frame and new 
# data frame into the rbind() function
df7=rbind(df9,df8)  
  
print("data frame after adding a new row")
print(df7)


Python3
# creating a data frame with some data
df = data.frame(id=c(1,2,3),
                name=c("maruti suzuki","tata","ford")) 
  
print("Original data frame")
print(df) 
  
# declaring a row of values in
# data.frame() function
df1 = data.frame(4,"volkswagen")
  
# adding names to the row values
names(df1)=c("id","name") 
  
# passing the original data frame and 
# new data frame into the rbind() function 
df2=rbind(df,df1) 
  
print("data frame after adding a new row")
print(df2)


R
# creating a data frame with some data
df3 = data.frame(id=c(1,2,3),
                 name=c("Asus","HP","Acer")) 
  
print("Original data frame")
print(df3) 
  
# declaring a row of values in 
# data.frame() function
df4 = data.frame(4,"Dell")
  
# adding names to the row values
names(df4)=c("id","name") 
  
# passing the original data frame and 
# new data frame into the rbind() function
df5=rbind(df3,df4) 
  
print("data frame after adding a new row")
print(df5)


输出 :

示例 2:

蟒蛇3

# creating a data frame with some data
df = data.frame(id=c(1,2,3),
                name=c("maruti suzuki","tata","ford")) 
  
print("Original data frame")
print(df) 
  
# declaring a row of values in
# data.frame() function
df1 = data.frame(4,"volkswagen")
  
# adding names to the row values
names(df1)=c("id","name") 
  
# passing the original data frame and 
# new data frame into the rbind() function 
df2=rbind(df,df1) 
  
print("data frame after adding a new row")
print(df2)

输出 :

示例 3:

电阻

# creating a data frame with some data
df3 = data.frame(id=c(1,2,3),
                 name=c("Asus","HP","Acer")) 
  
print("Original data frame")
print(df3) 
  
# declaring a row of values in 
# data.frame() function
df4 = data.frame(4,"Dell")
  
# adding names to the row values
names(df4)=c("id","name") 
  
# passing the original data frame and 
# new data frame into the rbind() function
df5=rbind(df3,df4) 
  
print("data frame after adding a new row")
print(df5) 

输出 :