如何替换 R 数据框中的特定值?
通常,我们数据框中的某些值不合适,它们不是最新的,或者我们不知道这些值。在这种情况下,我们替换这些值,因为它们会导致歧义。在这里,我们将使用术语 NA,它代表“不可用”来替换未知值。在本文中,我们将看到如何在 R 编程语言的帮助下更改或替换表中的所有特定值。
为此,需要有一个条件,在此基础上必须执行替换。根据此条件检查考虑中的数据,如果为 True,则由特定值替换。此值可以是已删除值或 NA 的数据类型的另一个值。让我们看看描述相同的各种实现。我们将讨论可以在特定列和完整数据框中替换特定值。
使用中的数据框:
替换单个列的值
为此,首先选择要对其进行更改的列,然后对其应用条件。条件为 True 的单元格将相应地替换。
例子:
R
# creates table with column names
# and values assigned to them
Name <- c('Adrian', 'Nathan','Heather',
'Abby', 'Delight', 'Hope',
'Lucifer', 'Faith',14,'Joseph')
RollNo <- c(24,23,14,18,29,56,14,39,12,20)
ID <- c(123, 336, 134, 148, 14, 289, 856,
773, 201, 536)
CPI <- c(8.5, 8.3, 7.8, 9.1, 7.9, 6.7, 8.3, 7.11,
7.9, 14.0)
HostelRoom <- c(23, 45, 31, 66, 40, 14, 23, 14, 52, 10)
# stores it in the form of data frame
# with the name StudentDetails
StudentDetails <- data.frame(Name,RollNo,ID,CPI,HostelRoom)
# saves the original dataframe with
# the name sdetails
sdetails <- StudentDetails
#values equal to 14 are replaced
# with NA
sdetails$HostelRoom[sdetails$HostelRoom == 14] <- NA
# views the modified table
View(sdetails)
R
# creates table with column names
# and values assigned to them
Name <- c('Adrian','Nathan','Heather',
'Abby','Delight','Hope',
'Lucifer','Faith',14,'Joseph')
RollNo <- c(24,23,14,18,29,56,14,39,12,20)
ID <- c(123, 336, 134, 148, 14, 289, 856,
773, 201, 536)
CPI <- c(8.5, 8.3, 7.8, 9.1, 7.9, 6.7, 8.3,
7.11, 7.9, 14.0)
HostelRoom <- c(23, 45, 31, 66, 40, 14, 23,
14, 52, 10)
# stores it in the form of data frame
# with the name StudentDetails
StudentDetails <- data.frame(Name,RollNo,ID,CPI,HostelRoom)
# saves the original dataframe with
# the name sdetails
sdetails <- StudentDetails
# values less than or equal to 20,
# will be replaced by NA
sdetails$RollNo[sdetails$RollNo<=20] <- "NA"
# views the modified table
View(sdetails)
R
# creates table with column names
# and values assigned to them
Name <- c('Adrian','Nathan','Heather',
'Abby','Delight','Hope',
'Lucifer','Faith',14,'Joseph')
RollNo <- c(24,23,14,18,29,56,14,39,12,20)
ID <- c(123, 336, 134, 148, 14, 289, 856,
773, 201, 536)
CPI <- c(8.5, 8.3, 7.8, 9.1, 7.9, 6.7, 8.3,
7.11, 7.9, 14.0)
HostelRoom <- c(23, 45, 31, 66, 40, 14, 23,
14, 52, 10)
# stores it in the form of data frame
# with the name StudentDetails
StudentDetails <- data.frame(Name,RollNo,ID,CPI,HostelRoom)
# saves the original dataframe with
# the name sdetails
sdetails <- StudentDetails
# Under CPI named column, 8.30 is replaced
# with 9.00 and 14.00 is replaced with 0.00
sdetails$CPI[sdetails$CPI == 8.30] <- 9.00
sdetails$CPI[sdetails$CPI == 14.00] <- 0.00
# views the modified table
View(sdetails)
R
# creates table with column names
# and values assigned to them
Name <- c('Adrian','Nathan','Heather',
'Abby','Delight','Hope',
'Lucifer','Faith',14,'Joseph')
RollNo <- c(24,23,14,18,29,56,14,39,12,20)
ID <- c(123, 336, 134, 148, 14, 289, 856,
773, 201, 536)
CPI <- c(8.5, 8.3, 7.8, 9.1, 7.9, 6.7, 8.3,
7.11, 7.9, 14.0)
HostelRoom <- c(23, 45, 31, 66, 40, 14, 23,
14, 52, 10)
# stores it in the form of data frame
# with the name StudentDetails
StudentDetails <- data.frame(Name,RollNo,ID,CPI,HostelRoom)
# saves the original dataframe with
# the name sdetails
sdetails <- StudentDetails
# replace the values numbered as 14,
# with NA in the entire dataframe
sdetails[sdetails == 14] <- NA
# views the modified table
View(sdetails)
输出:
也可以指定多个条件用于替换。
例子:
电阻
# creates table with column names
# and values assigned to them
Name <- c('Adrian','Nathan','Heather',
'Abby','Delight','Hope',
'Lucifer','Faith',14,'Joseph')
RollNo <- c(24,23,14,18,29,56,14,39,12,20)
ID <- c(123, 336, 134, 148, 14, 289, 856,
773, 201, 536)
CPI <- c(8.5, 8.3, 7.8, 9.1, 7.9, 6.7, 8.3,
7.11, 7.9, 14.0)
HostelRoom <- c(23, 45, 31, 66, 40, 14, 23,
14, 52, 10)
# stores it in the form of data frame
# with the name StudentDetails
StudentDetails <- data.frame(Name,RollNo,ID,CPI,HostelRoom)
# saves the original dataframe with
# the name sdetails
sdetails <- StudentDetails
# values less than or equal to 20,
# will be replaced by NA
sdetails$RollNo[sdetails$RollNo<=20] <- "NA"
# views the modified table
View(sdetails)
输出:
现有值也可以替换为相同数据类型的特定值。
例子:
电阻
# creates table with column names
# and values assigned to them
Name <- c('Adrian','Nathan','Heather',
'Abby','Delight','Hope',
'Lucifer','Faith',14,'Joseph')
RollNo <- c(24,23,14,18,29,56,14,39,12,20)
ID <- c(123, 336, 134, 148, 14, 289, 856,
773, 201, 536)
CPI <- c(8.5, 8.3, 7.8, 9.1, 7.9, 6.7, 8.3,
7.11, 7.9, 14.0)
HostelRoom <- c(23, 45, 31, 66, 40, 14, 23,
14, 52, 10)
# stores it in the form of data frame
# with the name StudentDetails
StudentDetails <- data.frame(Name,RollNo,ID,CPI,HostelRoom)
# saves the original dataframe with
# the name sdetails
sdetails <- StudentDetails
# Under CPI named column, 8.30 is replaced
# with 9.00 and 14.00 is replaced with 0.00
sdetails$CPI[sdetails$CPI == 8.30] <- 9.00
sdetails$CPI[sdetails$CPI == 14.00] <- 0.00
# views the modified table
View(sdetails)
输出:
替换整个数据帧的值
现在,我们将操作替换整个数据帧的值,而不考虑列。为此,将根据条件检查数据框中的所有值,并相应地替换结果为 True 的值。
程序:
电阻
# creates table with column names
# and values assigned to them
Name <- c('Adrian','Nathan','Heather',
'Abby','Delight','Hope',
'Lucifer','Faith',14,'Joseph')
RollNo <- c(24,23,14,18,29,56,14,39,12,20)
ID <- c(123, 336, 134, 148, 14, 289, 856,
773, 201, 536)
CPI <- c(8.5, 8.3, 7.8, 9.1, 7.9, 6.7, 8.3,
7.11, 7.9, 14.0)
HostelRoom <- c(23, 45, 31, 66, 40, 14, 23,
14, 52, 10)
# stores it in the form of data frame
# with the name StudentDetails
StudentDetails <- data.frame(Name,RollNo,ID,CPI,HostelRoom)
# saves the original dataframe with
# the name sdetails
sdetails <- StudentDetails
# replace the values numbered as 14,
# with NA in the entire dataframe
sdetails[sdetails == 14] <- NA
# views the modified table
View(sdetails)
输出: