📜  在 R DataFrame 中插入多行

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

在 R DataFrame 中插入多行

在本文中,我们将看到如何在 R 编程语言中在数据框中插入多行。首先,让我们创建一个DataFrame

要创建数据框,我们需要使用向量。我们需要创建具有一些值的向量,并将这些向量作为参数传递给 data.frame()函数。因此,创建了一个数据框。让我们看一个演示上述语句的示例。

示例 1:

R
# here num and branches are the vectors, ans those
# are passed into data.frame() as parameters .
dataframe = data.frame(num=c(1:3),branches=c("IT","CSE","ECE"))
  
# In this example we created vectors inside the
# data frame() function and assigned values to vectors. 
print(dataframe)


R
# creating a data frame
df1 = data.frame(num = c( 1 : 3), 
                 branch = c("IT", "CSE", "ECE"))
  
# creating another data frame
df2 = data.frame(num = c( 4 : 6), 
                 branch = c("EEE", "Mechanical", "civil"))
  
# selecting 1-2 rows , all columns from df1
new_row = df2[c(1, 2, 3),] 
# we can access data from a data frame through indexing .
# since it is a 2 dimensional one we can access data 
# by row number and column number .
# here c(1,2,3) represents row numbers and we left column
# numbers as empty . then all columns are accessed .
  
  
# new_row is appended to the df1
df1 = rbind(df1, new_row)
  
#printing updated data frame 
print(df1)


R
# creating one data frame
df1 = data.frame(num = c( 1 : 3 ),
                 branch = c("IT", "CSE", "ECE")) 
  
# creating another data frame
df2=data.frame(num = c( 4 : 6 ),
               branch = c("Chemical", "Petroleum", "Food Technology"))
  
df3=data.frame(num = c( 7 : 9 ),
               branch = c("EEE", "Mechanical", "Civil"))
  
# here we accessing 1-3 rows and all columns 
# from df2 and storing in new_row variable
new_row=df2[c(1, 2, 3),] 
  
# here also we are accessing 1-3 rows and all
# columns and storing in new_row2 variable
new_row2=df3[c(1, 2, 3),]
  
# passing data frame1 i.e., df1 and the new_row .  
df1=rbind(df1, new_row) 
  
# passing data frame1 i.e., df1 and new_row2
df1=rbind(df1, new_row2) 
  
# Here values in new_row will be appended with df1 . 
# if we pass df2,new_row ,
# the data in the nw_row will be appended with df2 
print(df1) # printing df1


输出:



在上面的例子中,我们创建了一个直接以向量作为参数的数据框。我们可以使用已经创建的向量创建数据框。让我们看一个例子。

将多行插入数据框中的步骤

  1. 创建一个数据框。
  2. 创建一个包含要添加到数据帧的行的向量。
  3. 使用以下方法向数据框中添加行。

执行 :

用于添加多行的预定义函数是 rbind()。我们必须传递一个数据框和一个包含数据行的向量。所以,让我们看看示例代码。

R 中数据帧的索引:

variable = df([ row,column ])

如果我们想提取多行,我们可以将行号放在一个向量中,并将该向量作为行或列传递。如果我们想提取 3 行和所有列,我们可以将行号放入向量中,并将列留空。下面的例子演示了上面的语句。

如果我们想提取特定的列,那么我们可以将列号放在一个向量中并作为参数传递。下面的例子演示了上面的语句。

这就是索引的工作方式。让我们看看 rbind()函数的介绍和用于将多行插入数据帧的实现。

rbind() 方法: rbind 表示行绑定。使用数据框连接多行。

示例 1:

电阻

# creating a data frame
df1 = data.frame(num = c( 1 : 3), 
                 branch = c("IT", "CSE", "ECE"))
  
# creating another data frame
df2 = data.frame(num = c( 4 : 6), 
                 branch = c("EEE", "Mechanical", "civil"))
  
# selecting 1-2 rows , all columns from df1
new_row = df2[c(1, 2, 3),] 
# we can access data from a data frame through indexing .
# since it is a 2 dimensional one we can access data 
# by row number and column number .
# here c(1,2,3) represents row numbers and we left column
# numbers as empty . then all columns are accessed .
  
  
# new_row is appended to the df1
df1 = rbind(df1, new_row)
  
#printing updated data frame 
print(df1)

输出:

示例 2:从 3 个数据框中添加行。

电阻

# creating one data frame
df1 = data.frame(num = c( 1 : 3 ),
                 branch = c("IT", "CSE", "ECE")) 
  
# creating another data frame
df2=data.frame(num = c( 4 : 6 ),
               branch = c("Chemical", "Petroleum", "Food Technology"))
  
df3=data.frame(num = c( 7 : 9 ),
               branch = c("EEE", "Mechanical", "Civil"))
  
# here we accessing 1-3 rows and all columns 
# from df2 and storing in new_row variable
new_row=df2[c(1, 2, 3),] 
  
# here also we are accessing 1-3 rows and all
# columns and storing in new_row2 variable
new_row2=df3[c(1, 2, 3),]
  
# passing data frame1 i.e., df1 and the new_row .  
df1=rbind(df1, new_row) 
  
# passing data frame1 i.e., df1 and new_row2
df1=rbind(df1, new_row2) 
  
# Here values in new_row will be appended with df1 . 
# if we pass df2,new_row ,
# the data in the nw_row will be appended with df2 
print(df1) # printing df1

输出: