📜  在R中逐行创建DataFrame

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

在R中逐行创建DataFrame

在本文中,我们将讨论如何在 R 编程语言中逐行创建数据帧。

方法一:使用for循环和索引方法

可以使用 R 中的 data.frame() 方法创建 R 语言中的空数据框。为了更清晰,可以在声明期间定义列的数据类型。数据框的每一行都是一个向量,由属于不同列的值组成。在数据帧中的第i行随后可以分配给该载体。

句法:

行被附加到数据帧的末尾。行的添加也可以在 for 循环中完成,循环迭代相当于要添加的行数。附加向量的长度应等于数据框中的列数。



例子:

R
# declaring an empty data frame
data_frame = data.frame(
  col1 = numeric(), col2 = character(),stringsAsFactors = FALSE)
print ("Original dataframe")
print (data_frame)
 
# appending rows to the data frame
for(i in 1:3) {                      
   
  # creating a vector to append to
  # data frame
  vec <- c(i+1, LETTERS[i])    
  
  # assigning this vector to ith row
  data_frame[i, ] <- vec          
}
 
print ("Modified dataframe")
print (data_frame)


R
# declaring an empty data frame
data_frame = data.frame(col1 = numeric(),
                        col2 = character(),
                        stringsAsFactors = FALSE)
 
print ("Original dataframe")
print (data_frame)
 
# appending rows to the data frame
data_frame[1,] <- c(1,"firstrow")
 
print ("Dataframe : Iteration 1")
print (data_frame)
 
# appending rows to the data frame
data_frame[2,] <- c(2,"secondrow")
print ("Dataframe : Iteration 2")
print (data_frame)
 
data_frame[3,] <- c(3,"thirdrow")
print ("Dataframe : Iteration 3")
print (data_frame)


R
# declaring an empty data frame
data_frame = data.frame(col1 = c(2:3),
                        col2 = letters[1:2],
                        stringsAsFactors = FALSE)
 
print ("Original dataframe")
print (data_frame)
 
# appending rows at the end
vec <- c(8,"n")
data_frame[nrow(data_frame)+1,] <- vec
 
print ("Modified dataframe")
print (data_frame)


R
# declaring an empty data frame
data_frame = data.frame(col1 = numeric(),
                        col2 = character(),
                        stringsAsFactors = FALSE)
 
print ("Original dataframe")
print (data_frame)
 
# appending rows at the end
vec <- c(8,"n")
 
# binding row at the end of the data frame
data_frame <- rbind(data_frame,vec,stringsAsFactors = FALSE)
 
print ("Modified dataframe")
print (data_frame)


R
# declaring an empty data frame
data_frame = data.frame(col1 = numeric(),
                        col2 = character(),
                        stringsAsFactors = FALSE)
 
print ("Original dataframe")
print (data_frame)
 
# appending rows at the end
for(i in 1:3) {
   
  # creating a vector to append
  # to data frame
  vec <- c(i, LETTERS[i])
  
  # assigning this vector to ith row
  data_frame <- rbind(data_frame,vec,stringsAsFactors=FALSE)
}
 
print ("Modified dataframe")
print (data_frame)


输出

[1] "Original dataframe"
[1] col1 col2
<0 rows> (or 0-length row.names)
[1] "Modified dataframe"
 col1 col2
1    2    A
2    3    B
3    4    C

方法二:使用手动插入

也可以使用行索引引用插入空数据框中的行。每次迭代后,都会在数据框中追加一个新行。对原始数据框进行了修改。

例子:

电阻

# declaring an empty data frame
data_frame = data.frame(col1 = numeric(),
                        col2 = character(),
                        stringsAsFactors = FALSE)
 
print ("Original dataframe")
print (data_frame)
 
# appending rows to the data frame
data_frame[1,] <- c(1,"firstrow")
 
print ("Dataframe : Iteration 1")
print (data_frame)
 
# appending rows to the data frame
data_frame[2,] <- c(2,"secondrow")
print ("Dataframe : Iteration 2")
print (data_frame)
 
data_frame[3,] <- c(3,"thirdrow")
print ("Dataframe : Iteration 3")
print (data_frame)

输出

[1] "Original dataframe"
[1] col1 col2
<0 rows> (or 0-length row.names)
[1] "Dataframe : Iteration 1"
 col1     col2
1    1 firstrow
[1] "Dataframe : Iteration 2"
 col1      col2
1    1  firstrow
2    2 secondrow
[1] "Dataframe : Iteration 3"
 col1      col2
1    1  firstrow
2    2 secondrow
3    3  thirdrow

可以通过 nrow(dataframe) 方法捕获数据帧的当前行数。可以在 nrow(df)+1 索引处将单个行添加到数据框中。



句法:

例子:

电阻

# declaring an empty data frame
data_frame = data.frame(col1 = c(2:3),
                        col2 = letters[1:2],
                        stringsAsFactors = FALSE)
 
print ("Original dataframe")
print (data_frame)
 
# appending rows at the end
vec <- c(8,"n")
data_frame[nrow(data_frame)+1,] <- vec
 
print ("Modified dataframe")
print (data_frame)

输出

[1] "Original dataframe"
 col1 col2
1    2    a
2    3    b
[1] "Modified dataframe"
 col1 col2
1    2    a
2    3    b
3    8    n

方法 3:使用rbind()方法

rbind() 方法用于将向量或数据框对象绑定在一起以形成更大的对象。最初,我们可以创建一个空数据框,然后使用向量定义一个新行,并使用 rbind() 方法将声明的行绑定到数据框。新行附加在末尾。该行的数据类型应与为原始数据框声明的数据类型兼容。这可以包含在循环中以在每次循环迭代期间绑定行。

句法:

例子:

电阻

# declaring an empty data frame
data_frame = data.frame(col1 = numeric(),
                        col2 = character(),
                        stringsAsFactors = FALSE)
 
print ("Original dataframe")
print (data_frame)
 
# appending rows at the end
vec <- c(8,"n")
 
# binding row at the end of the data frame
data_frame <- rbind(data_frame,vec,stringsAsFactors = FALSE)
 
print ("Modified dataframe")
print (data_frame)

输出

[1] "Original dataframe"
[1] col1 col2
<0 rows> (or 0-length row.names)
[1] "Modified dataframe"
  X.8. X.n.
1    8    n

也可以逐行创建数据框,在 for 循环中对数据框使用重复的 rbind() 操作,迭代次数等于要插入的行数。

例子:

电阻

# declaring an empty data frame
data_frame = data.frame(col1 = numeric(),
                        col2 = character(),
                        stringsAsFactors = FALSE)
 
print ("Original dataframe")
print (data_frame)
 
# appending rows at the end
for(i in 1:3) {
   
  # creating a vector to append
  # to data frame
  vec <- c(i, LETTERS[i])
  
  # assigning this vector to ith row
  data_frame <- rbind(data_frame,vec,stringsAsFactors=FALSE)
}
 
print ("Modified dataframe")
print (data_frame)

输出

[1] "Original dataframe"
[1] col1 col2
<0 rows> (or 0-length row.names)
[1] "Modified dataframe"
  X.1. X.A.
1    1    A
2    2    B
3    3    C