📌  相关文章
📜  如何替换 R DataFrame 列中的特定值?

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

如何替换 R DataFrame 列中的特定值?

在本文中,我们将看到如何在 R 编程语言中替换 DataFrame 列中的特定值。

方法一:使用Replace()函数。

R 语言中的 replace()函数用于将指定字符串向量 x 中的值替换为列表中给出的索引值中给出的值。

它有三个参数,首先是列表名称,然后是需要替换元素的索引,第三个参数是替换值。



例子:

R
# List of Names
Names<-c("Suresh","Sita","Anu","Manasa",
         "Riya","Ramesh","Roopa","Neha")
 
# Roll numbers
Roll_No<-1:8
 
# Marks obtained
Marks<-c(15, 20, 3, -1, 14, -2 , 10, 13)
Full_Marks<-c(20, 10, 44, 21, 24, 36, 20, 13)
 
# df name of data frame
# Converting the list into dataframe
df<-data.frame(Roll_No,Names, Marks, Full_Marks)
 
print("Original DF")
print(df)
 
print("Replaced Value")
 
#replaces the negative numbers with zeros
data<-replace(df$Marks, df$Marks<0, 0)
print(data)


R
# List of Names
Names<-c("Suresh","Sita","Anu","Manasa",
         "Riya","Ramesh","Roopa","Neha")
 
# Roll numbers
Roll_No<-1:8
 
# Marks obtained
Marks<-c(15, 20, 3, 11, 14, 16, 10, 13) 
 
# df name of data frame
# Converting the list into dataframe
df<-data.frame(Roll_No,Names,Marks)
 
print(df)


R
# List of Names
Names<-c("Suresh","Sita","Anu","Manasa",
         "Riya","Ramesh","Roopa","Neha")
 
# Roll numbers
Roll_No<-1:8
 
# Marks obtained
Marks<-c(15, 20, 3, 11, 14, 16, 10, 13) 
 
# df name of data frame
# Converting the list into dataframe
df<-data.frame(Roll_No,Names, Marks)
 
df$Marks[df$Names == "Sita"] <- 25
# Column_name1 is Marks which we need to replace
 
# Print the modified data frame
print(df)


R
# List of Names
Names<-c("Suresh","Sita","Anu","Manasa",
         "Riya","Ramesh","Roopa","Neha")
 
# Roll numbers
Roll_No<-1:8
 
# Marks obtained
Marks<-c(15, 20, 3, 11, 14, 16, 10, 13) 
 
# df name of data frame
# Converting the list into dataframe
df<-data.frame(Roll_No,Names,Marks)
 
df$Names[df$Names=="Ramesh"]<-"Vikas"
 
# Prints the modified data frame
print(df)


输出:

[1] "Original DF"
  Roll_No  Names Marks Full_Marks
1       1 Suresh    15         20
2       2   Sita    20         10
3       3    Anu     3         44
4       4 Manasa    -1         21
5       5   Riya    14         24
6       6 Ramesh    -2         36
7       7  Roopa    10         20
8       8   Neha    13         13
[1] "Replaced Value"
15 20 3 0 14 0 10 13

您可以在上面的代码中看到,我们将第二个元素从“Sonam”替换为“Harish”。

方法二:使用逻辑条件。

现在让我们看看如何使用逻辑条件替换列中特定值的值。首先,让我们在 R 中创建数据框

电阻

# List of Names
Names<-c("Suresh","Sita","Anu","Manasa",
         "Riya","Ramesh","Roopa","Neha")
 
# Roll numbers
Roll_No<-1:8
 
# Marks obtained
Marks<-c(15, 20, 3, 11, 14, 16, 10, 13) 
 
# df name of data frame
# Converting the list into dataframe
df<-data.frame(Roll_No,Names,Marks)
 
print(df)

输出:



数据帧

现在,让我们看看如何替换列中的特定值。

示例 1:在我们的数据框中, “Sita”标记为20,让我们将其替换为25

代码:

电阻

# List of Names
Names<-c("Suresh","Sita","Anu","Manasa",
         "Riya","Ramesh","Roopa","Neha")
 
# Roll numbers
Roll_No<-1:8
 
# Marks obtained
Marks<-c(15, 20, 3, 11, 14, 16, 10, 13) 
 
# df name of data frame
# Converting the list into dataframe
df<-data.frame(Roll_No,Names, Marks)
 
df$Marks[df$Names == "Sita"] <- 25
# Column_name1 is Marks which we need to replace
 
# Print the modified data frame
print(df)

输出:

Sita 标记被替换

在上面的代码中,您可以看到我们在数据框“df”中使用了两列(Marks、Names) 第一的, 我们获取需要替换的数据,在我们的例子中,我们需要替换一个名为“Sita”的人的标记。因此,我们通过标记列并停止与这些标记相连的名称是“Sita”。找到该位置后,我们将标记替换为 25

示例 2:

让我们将“Ramesh”的名称替换为“Vikas”。

电阻

# List of Names
Names<-c("Suresh","Sita","Anu","Manasa",
         "Riya","Ramesh","Roopa","Neha")
 
# Roll numbers
Roll_No<-1:8
 
# Marks obtained
Marks<-c(15, 20, 3, 11, 14, 16, 10, 13) 
 
# df name of data frame
# Converting the list into dataframe
df<-data.frame(Roll_No,Names,Marks)
 
df$Names[df$Names=="Ramesh"]<-"Vikas"
 
# Prints the modified data frame
print(df)

输出:

拉梅什被维卡斯换下

在上面的代码中,您可以看到我们正在获取名称为“Ramesh”的行,因此就像在列表中进行线性搜索以查找给定元素的位置在找到该位置后,我们将名称替换为“Vikas” ”。