Find and Replace values in R

352 views Asked by At

I would like to find a specific value in a column of a data frame and then replaced by whatever I want.

For example, I have a data frame with the name of cities (column 1) and the frequency (column 2). But some of the cities have different district thus R see them like different cities because they have not the same row name.

Example:

--> I have:

      City     Freq
1    Paris 01   69
2    Paris 03   60
3    Paris 15   12
4    Paris 20   2
5    Toulouse   60
6    Paris      15
7    Lille      12

--> I would like:

      City Freq
1    Paris   69
2 Toulouse   60
3    Lille   12

I tried to use the gsub function but I don't know how to deal with it. Also I tried some if statement but I failed. I really tried to find some answers before posting something but the examples I have found are simplier and involved only the change of all the column (etc...).

Thank you for helping me!

Here some informations about my data:

dput(droplevels(head(data))) 

structure(list(City = structure(c(1L, 4L, 3L, 5L, 2L, 6L), .Label = c("PARIS", "PARIS 13", "PARIS 15", "PARIS 16", "PARIS 18", "PARIS 20"), class = "factor"), Freq = c(8859L, 3843L, 3583L, 2651L, 2586L, 2464L)), .Names = c("City", "Freq"), row.names = c(19380L, 19396L, 19395L, 19398L, 19393L, 19400L), class = "data.frame")
1

There are 1 answers

4
akrun On BEST ANSWER

You can modify the 'City' column using sub

df2 <- transform(df1, City=tolower(sub("\\s+.*$", '', City)))
res <- aggregate(Freq~City,df2, FUN=sum)
res
#     City Freq
#1    lille   12
#2    paris   69
#3 toulouse   60

res$City <- sprintf('%s%s', toupper(substr(res$City,1,1)),
                 sub('^.', '', res$City))

data

df1 <- structure(list(City = structure(c(3L, 4L, 5L, 6L, 7L, 2L, 1L), 
.Label = c("Lille", 
"Paris", "Paris 01", "Paris 03", "Paris 15", "PARIS 20", "Toulouse"
), class = "factor"), Freq = c(12, 15, 25, 2, 60, 15, 12)),
.Names =    c("City", 
"Freq"), row.names = c(NA, -7L), class = "data.frame")