Replace Characters from a Column of a Data Frame R

how to replace character in specific columns of data frame

I think you can use the following solution:

library(dplyr)

df %>%
mutate(across(c(Sold.Price, Price, MaintFee), ~ as.numeric(gsub("[$,]", "", .x))))

Sold.Price Title.to.Land Price DOM List.Date MaintFee
1 NA Freehold Strata 174900 93 10/4/2019 2916.00
2 177500 Freehold Strata 177500 34 12/12/2019 373.80
3 180000 Freehold Strata 180000 39 12/9/2019 331.57
4 180000 Freehold Strata 180000 56 11/12/2019 320.42
5 189000 Freehold Strata 189000 2 1/9/2020 1055.67

Or in base R we could do:

as.data.frame(sapply(df, function(x) {
if(any(grepl("\\$", x))) {
as.numeric(gsub("[$,]", "", x))
} else {
x
}
}))

Replace character in a dataframe with another character

It is the proverbial stringsAsFactors=FALSE. For those reading it after R4.0 it is no longer a problem, but for many years before 2020 users struggled remembering that data.frame (and as.data.frame() for that matter) automatically coerces all strings to factors.

What then happens is that you are trying to introduce new levels into a factor and this is not how it needs to be done in R. If creation of factor was not an intention, you could just modify your data frame creation code.

df <- data.frame(N=c(1,2,3,4,5,6),
CAT=c('A','B','C','D','E','F'),
stringsAsFactors = FALSE)

If you, however, wanted to create a factor, here's how you can go about modifying the levels and recoding one of the levels.

df <- data.frame(N=c(1,2,3,4,5,6),
CAT=c('A','B','C','D','E','F'),
stringsAsFactors = TRUE)
df
str(df)
#> 'data.frame': 6 obs. of 2 variables:
#> $ N : num 1 2 3 4 5 6
#> $ CAT: Factor w/ 6 levels "A","B","C","D",..: 1 2 3 4 5 6

levels(df$CAT)[levels(df$CAT)=="F"] <- "X"

df

#> N CAT
#> 1 1 A
#> 2 2 B
#> 3 3 C
#> 4 4 D
#> 5 5 E
#> 6 6 X

Replace all occurrences of a string in a data frame

If you are only looking to replace all occurrences of "< " (with space) with "<" (no space), then you can do an lapply over the data frame, with a gsub for replacement:

> data <- data.frame(lapply(data, function(x) {
+ gsub("< ", "<", x)
+ }))
> data
name var1 var2
1 a <2 <3
2 a <2 <3
3 a <2 <3
4 b <2 <3
5 b <2 <3
6 b <2 <3
7 c <2 <3
8 c <2 <3
9 c <2 <3

Replace characters with dates in dataframe in r

Personally I would use dplyr to mutate the values of the original cell. In combination with lubridate it works for me (at least I think this what you wanted):

df <- df %>% mutate(date =ymd_hms(strptime(date, "%d.%m.%Y %H:%M:%S"))) %>% arrange(date)

                 date   id
1 2019-10-20 10:12:20 1250
2 2019-10-21 10:12:16 1238
3 2019-11-20 10:12:15 1234

R - Dataframe - Replace characters in a string using two columns containing lists of positions and characters

The nesting is one level deep. So, extract that element and loop

for(i in seq_along(df$pos[[1]])) {
substring(df$string[[1]], df$pos[[1]][i],
df$pos[[1]][i]) <- df$charlist[[1]][i]
}

-output

df$string
#$string
#[1] "macaroni"

If there are more rows, do a nested loop

for(i in seq_along(df$pos)) {
for(j in seq_along(df$pos[[i]])) {
substring(df$string[[i]], df$pos[[i]][j],
df$pos[[i]][j]) <- df$charlist[[i]][j]
}
}

df$string
#$string
#[1] "macaroni"

Replace specific characters in a variable in data frame in R

You can use the special groups [:punct:] and [:space:] inside of a pattern group ([...]) like this:

df <- data.frame(
DMA.NAME = c(
"Columbus, OH",
"Orlando-Daytona Bch-Melbrn",
"Boston (Manchester)",
"Columbus, OH",
"Orlando-Daytona Bch-Melbrn",
"Minneapolis-St. Paul"),
stringsAsFactors=F)
##
> gsub("[[:punct:][:space:]]+","\\.",df$DMA.NAME)
[1] "Columbus.OH" "Orlando.Daytona.Bch.Melbrn" "Boston.Manchester." "Columbus.OH"
[5] "Orlando.Daytona.Bch.Melbrn" "Minneapolis.St.Paul"


Related Topics



Leave a reply



Submit