使用gsub将R中的句点替换为空白,但保留其他小数
我使用gsub将此数据框中列中的句点替换为空白。然而,一个挑战是,在某些列中,数字数据中包含不应删除的句点,因为它们用于小数位数使用gsub将R中的句点替换为空白,但保留其他小数,r,R,我使用gsub将此数据框中列中的句点替换为空白。然而,一个挑战是,在某些列中,数字数据中包含不应删除的句点,因为它们用于小数位数 MeasurementCleaned <- gsub("[.]","",df$Measurement) 其他答案完全符合您的要求,但我过去经常处理格式与此完全相同的数据。我发现,如果我从文件中读取数据帧,最好使用na.strings选项来防止点首先出现: df <- read.csv('my_data.csv', na.strings='.', str
MeasurementCleaned <- gsub("[.]","",df$Measurement)
其他答案完全符合您的要求,但我过去经常处理格式与此完全相同的数据。我发现,如果我从文件中读取数据帧,最好使用
na.strings
选项来防止点首先出现:
df <- read.csv('my_data.csv', na.strings='.', stringsAsFactors=FALSE)
df其他答案完全可以满足您的要求,但我过去经常处理格式与此完全相同的数据。我发现,如果我从文件中读取数据帧,最好使用na.strings
选项来防止点首先出现:
df <- read.csv('my_data.csv', na.strings='.', stringsAsFactors=FALSE)
df看起来像ifelse(df$measurement==”,“,”,df$measurement)
会实现你设定的目标,但你可能只想做as.numeric(df$measurement)
而不是像ifelse(df$measurement==”,“,df$measurement)
会实现你设定的目标,但是您可能只想将改为.numeric(df$Measurement)
?
sub("^[.]$","",df$Measurement)
[1] "" "33.2" "32.5" ""
df$MeasurementCleaned=sub("^\\.$","",df$Measurement)
df
DataID Measurement MeasurementCleaned
1 1 .
2 2 33.2 33.2
3 3 32.5 32.5
4 4 .
df <- read.csv('my_data.csv', na.strings='.', stringsAsFactors=FALSE)