📜  将索引 ID 添加到 R 中的 DataFrame

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

将索引 ID 添加到 R 中的 DataFrame

在本文中,我们将讨论如何在 R 编程语言中将索引 ID 添加到数据帧中。

方法 1:使用cbind()nrow()方法

R 编程语言中的 nrow() 方法用于计算指定为该方法参数的数据帧中的行数。 R 语言中的 cbind() 方法用于将向量附加到数据帧。向量按照在函数调用期间指定的顺序附加到数据帧。为了使用 id 向量引导数据帧,将使用以下语法:

向量长度应等于数据框中的行数。

例子:



R
# declaring a data frame in R
data_frame <- data.frame(x1 = 2:7,            
                   x2 = letters[1:6],
                   x3 = 6,
                   row.names = c('I','II','III','IV','V','VI'))
 
print("Original Data Frame")                  
print(data_frame)
 
# number of rows in data frame
num_rows = nrow(data_frame)
  
# creating ID column vector
ID <- c(1:num_rows)
 
# binding id column to the data frame
data_frame1 <- cbind(ID , data_frame)
 
print("Modified Data Frame")
print (data_frame1)


R
# declaring a data frame in R
data_frame <- data.frame(x1 = 2:7,            
                   x2 = letters[1:6],
                   x3 = 6,
                   row.names = c('I','II','III','IV','V','VI')
                  )
 
print("Original Data Frame")                  
print(data_frame)
 
# number of rows in data frame
num_rows = nrow(data_frame)
 
# changing row names of the data frame
rownames(data_frame) <- c(1:num_rows)
 
print("Modified Data Frame")
print (data_frame)


R
# declaring a data frame in R
data_frame <- data.frame(x1 = 2:7,            
                   x2 = letters[1:6],
                   x3 = 6,
                   row.names = c('I','II','III','IV','V','VI')
                  )
 
print("Original Data Frame")                  
print(data_frame)
 
# number of rows in data frame
num_rows = nrow(data_frame)
 
# creating ID column vector
data_frame$ID <- seq.int(num_rows)
 
print("Modified Data Frame")
print (data_frame)


R
library(dplyr)
 
data_frame <- data.frame(x1 = 2:7,            
                         x2 = letters[1:6],
                         x3 = 6
                         )
 
print("Original Data Frame")                  
print(data_frame)
 
data_frame <- data_frame %>% mutate(ID = row_number())
 
print("Modified Data Frame")                  
print(data_frame)


输出

[1] "Original Data Frame"
   x1 x2 x3
I    2  a  6
II   3  b  6
III  4  c  6
IV   5  d  6
V    6  e  6
VI   7  f  6
[1] "Modified Data Frame"
   ID x1 x2 x3
I    1  2  a  6
II   2  3  b  6
III  3  4  c  6
IV   4  5  d  6
V    5  6  e  6
VI   6  7  f  6

方法 2:在数据框中分配行名称作为索引 ID

为了用索引 ID 列引导数据框,我们还可以重新分配数据框的行名称,以反映从 1 开始到数据框中行数的递增整数值。 rownames(df) 方法用于分配行名称。所有更改都反映在原始数据框中。

例子:

电阻

# declaring a data frame in R
data_frame <- data.frame(x1 = 2:7,            
                   x2 = letters[1:6],
                   x3 = 6,
                   row.names = c('I','II','III','IV','V','VI')
                  )
 
print("Original Data Frame")                  
print(data_frame)
 
# number of rows in data frame
num_rows = nrow(data_frame)
 
# changing row names of the data frame
rownames(data_frame) <- c(1:num_rows)
 
print("Modified Data Frame")
print (data_frame)

输出

[1] "Original Data Frame"
   x1 x2 x3
I    2  a  6
II   3  b  6
III  4  c  6
IV   5  d  6
V    6  e  6
VI   7  f  6
[1] "Modified Data Frame"
 x1 x2 x3
1  2  a  6
2  3  b  6
3  4  c  6
4  5  d  6
5  6  e  6
6  7  f  6

方法 3:使用seq.int()方法

R 中的 seq.int() 方法用于生成从 1 开始到指定为函数参数的数字 x 的整数序列。行名称已相关。新添加的列附加在数据框的末尾。

句法:



例子:

电阻

# declaring a data frame in R
data_frame <- data.frame(x1 = 2:7,            
                   x2 = letters[1:6],
                   x3 = 6,
                   row.names = c('I','II','III','IV','V','VI')
                  )
 
print("Original Data Frame")                  
print(data_frame)
 
# number of rows in data frame
num_rows = nrow(data_frame)
 
# creating ID column vector
data_frame$ID <- seq.int(num_rows)
 
print("Modified Data Frame")
print (data_frame)

输出

[1] "Original Data Frame"
   x1 x2 x3
I    2  a  6
II   3  b  6
III  4  c  6
IV   5  d  6
V    6  e  6
VI   7  f  6
[1] "Modified Data Frame"
   x1 x2 x3 ID
I    2  a  6  1
II   3  b  6  2
III  4  c  6  3
IV   5  d  6  4
V    6  e  6  5
VI   7  f  6  6

方法四:使用dplyr库

dplyr 包的 mutate 方法可用于向包含的数据框对象添加、删除和修改更多数据。为了添加新列,可以使用以下变异方法的变体:

句法:

其中逻辑指定数据添加所基于的条件

这里,row_number() 方法用于提供递增的整数序列来存储行号。新添加的列附加在现有数据对象的末尾。

例子:

电阻

library(dplyr)
 
data_frame <- data.frame(x1 = 2:7,            
                         x2 = letters[1:6],
                         x3 = 6
                         )
 
print("Original Data Frame")                  
print(data_frame)
 
data_frame <- data_frame %>% mutate(ID = row_number())
 
print("Modified Data Frame")                  
print(data_frame)

输出

[1] "Original Data Frame" 
   x1 x2 x3 
1  2  a  6 
2  3  b  6 
3  4  c  6 
4  5  d  6 
5  6  e  6 
6  7  f  6 
[1] "Modified Data Frame" 
   x1 x2 x3 ID 
1  2  a  6  1 
2  3  b  6  2 
3  4  c  6  3 
4  5  d  6  4 
5  6  e  6  5 
6  7  f  6  6