# How to replace $sign combined with some specific values in an R data frame? Sometimes we get very dirty data and that is the reason data analysis is a difficult task. Most of the data scientists look for clean data but it is almost impossible due to data warehouses often just focus on the data availability instead of the quality of data. One of the head scratching situations is getting an unnecessary value placed at different position in a random manner,$ sign is also a that type of value. We can remove this from an R data frame by using lapply function.

## Example

Consider the below data frame:

Live Demo

> x<-sample(c("A","$B","C"),20,replace=TRUE) > y<-sample(c("I","II","$II"),20,replace=TRUE)
> df1<-data.frame(x,y)
> df1

## Output

x y
1 C $II 2 C II 3 A I 4$B $II 5$B $II 6 A I 7 A$II
8 C I
9 $B II 10$B II
11 C $II 12 A II 13$B II
14 C I
15 C $II 16 C I 17 C II 18$B I
19 $B II 20 C$II

> df1

## Output

 "II" "II" "I" "II" "II" "I" "II" "I" "II" "II" "II" "II" "II" "I" "II"
 "I" "II" "I" "II" "II"

Let’s have a look at another example:

## Example

Live Demo

> Price<-sample(c("1$","2$","3$","4$"),20,replace=TRUE)
> Group<-sample(c("$First","$Second","Third"),20,replace=TRUE)
> df2<-data.frame(Price,Group)
> df2

## Output

Price Group
1 3Second
2 2$Third 3 1$ Third
4 2Second
5 2First
6 4First
7 2First
8 3First
9 2$Third 10 4$ Third
11 3First
12 3$Third 13 3$ $Second 14 2$ $First 15 4$ Third
16 3First
17 4$Third 18 2$ $First 19 2$ $Second 20 3$ Third

> df2

## Output

$Price  "3" "2" "1" "2" "2" "4" "2" "3" "2" "4" "3" "3" "3" "2" "4" "3" "4" "2" "2"  "3"$Group
 "Second" "Third" "Third" "Second" "First" "First" "First" "First"
 "Third" "Third" "First" "Third" "Second" "First" "Third" "First"
 "Third" "First" "Second" "Third"

Updated on: 19-Nov-2020

53 Views
{"ft_lang_label":"__label__en","ft_lang_prob":0.80019546,"math_prob":0.8307351,"size":2679,"snap":"2023-40-2023-50","text_gpt3_token_len":980,"char_repetition_ratio":0.14429906,"word_repetition_ratio":0.016427105,"special_character_ratio":0.3904442,"punctuation_ratio":0.069418386,"nsfw_num_words":0,"has_unicode_error":false,"math_prob_llama3":0.9798493,"pos_list":[0,1,2],"im_url_duplicate_count":[null,null,null],"WARC_HEADER":"{\"WARC-Type\":\"response\",\"WARC-Date\":\"2023-12-11T19:47:35Z\",\"WARC-Record-ID\":\"<urn:uuid:73b90c99-1f5c-4571-b33f-04463264a00e>\",\"Content-Length\":\"85255\",\"Content-Type\":\"application/http; msgtype=response\",\"WARC-Warcinfo-ID\":\"<urn:uuid:5dbb3b8d-ce53-4a88-9649-6fbb137147b2>\",\"WARC-Concurrent-To\":\"<urn:uuid:5519f89b-76e4-4630-8aa1-348758e30f1d>\",\"WARC-IP-Address\":\"192.229.210.176\",\"WARC-Target-URI\":\"https://www.tutorialspoint.com/how-to-replace-sign-combined-with-some-specific-values-in-an-r-data-frame\",\"WARC-Payload-Digest\":\"sha1:YG7JJNG7RFTCSPCFH6KD3HDAWQGWXWXZ\",\"WARC-Block-Digest\":\"sha1:STDIZEM424IJO6Y6ICRXIB6XK6NYIW2F\",\"WARC-Identified-Payload-Type\":\"text/html\",\"warc_filename\":\"/cc_download/warc_2023/CC-MAIN-2023-50/CC-MAIN-2023-50_segments_1700679516047.98_warc_CC-MAIN-20231211174901-20231211204901-00795.warc.gz\"}"}