R语言 如何替换R数据帧中的特定值
通常,我们的数据框架中的一些值是不合适的,它们不是最新的,或者我们不知道这些值。在这种情况下,我们要替换这些值,因为它们会引起歧义。在这里,我们将使用NA这个术语,它代表不可用,以替换未知的值。在这篇文章中,我们将看到如何在R编程语言的帮助下,改变或替换一个表中的所有特定值。
要做到这一点,需要有一个条件,在此基础上必须进行替换。所考虑的数据要根据这个条件进行检查,如果发现是 “真”,就用一个特定的值来替换。这个值可以是被删除值的另一个数据类型的值,也可以是NA。让我们来看看描述相同情况的各种实现。我们将讨论一个特定的值可以在一个特定的列和整个数据框中被替换。
使用中的数据框架
学生详情
替换单个列的值
首先选择要进行更改的那一列,然后将条件应用于它。条件为 “真 “的单元格将被相应替换。
例子
# 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
sdetailsHostelRoom[sdetailsHostelRoom == 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
sdetailsRollNo[sdetailsRollNo<=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
sdetailsCPI[sdetailsCPI == 8.30] <- 9.00
sdetailsCPI[sdetailsCPI == 14.00] <- 0.00
# 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
# replace the values numbered as 14,
# with NA in the entire dataframe
sdetails[sdetails == 14] <- NA
# views the modified table
View(sdetails)
输出
替换后的数值