Sunday, January 15, 2023

Useful commands for R

How to remove or replace a comma or sign from a dataset


y_mod<- gsub("\\,", "", y)

> y

 [1] "$133,172" "$129,201" "$127,575" "$124,679" "$121,280" "$120,390"

 [7] "$118,391" "$117,548" "$116,638" "$116,564" "$116,434" "$116,253"

[13] "$116,252" "$113,536" "$113,325" "$112,851" "$112,813" "$112,746"

[19] "$112,238" "$112,151" "$112,113" "$111,346" "$109,286" "$109,256"

[25] "$108,610" "$106,369" "$106,296" "$105,665" "$104,159" "$103,247"

[31] "$103,087" "$102,923" "$102,736" "$102,461" "$101,968" "$101,620"

[37] "$101,284" "$101,195" "$99,712"  "$99,276"  "$98,776"  "$98,052" 

[43] "$97,389"  "$96,564"  "$94,548"  "$94,428"  "$93,556"  "$93,085" 

[49] "$89,464"  "$84,706" 


Then we use

> gsub("\\.", "", y)

 [1] "$133172" "$129201" "$127575" "$124679" "$121280" "$120390"

 [7] "$118391" "$117548" "$116638" "$116564" "$116434" "$116253"

[13] "$116252" "$113536" "$113325" "$112851" "$112813" "$112746"

[19] "$112238" "$112151" "$112113" "$111346" "$109286" "$109256"

[25] "$108610" "$106369" "$106296" "$105665" "$104159" "$103247"

[31] "$103087" "$102923" "$102736" "$102461" "$101968" "$101620"

[37] "$101284" "$101195" "$99712"  "$99276"  "$98776"  "$98052" 

[43] "$97389"  "$96564"  "$94548"  "$94428"  "$93556"  "$93085" 

[49] "$89464"  "$84706"

> gsub("\\,", "", y)               #this replaces the comma by an empty space



>y_mod<- gsub("\\$", "", y_mod)

> y_mod

 [1] "133172" "129201" "127575" "124679" "121280" "120390" "118391"

 [8] "117548" "116638" "116564" "116434" "116253" "116252" "113536"

[15] "113325" "112851" "112813" "112746" "112238" "112151" "112113"

[22] "111346" "109286" "109256" "108610" "106369" "106296" "105665"

[29] "104159" "103247" "103087" "102923" "102736" "102461" "101968"

[36] "101620" "101284" "101195" "99712"  "99276"  "98776"  "98052" 

[43] "97389"  "96564"  "94548"  "94428"  "93556"  "93085"  "89464" 

[50] "84706" 

> y_mod<- gsub("\\$", "", y_mod)             #this replaces the dollar sign by an empty space


Strings to numeric

new <- as.numeric(y_mod)

 [1] 133172 129201 127575 124679 121280 120390 118391 117548 116638

[10] 116564 116434 116253 116252 113536 113325 112851 112813 112746

[19] 112238 112151 112113 111346 109286 109256 108610 106369 106296

[28] 105665 104159 103247 103087 102923 102736 102461 101968 101620

[37] 101284 101195  99712  99276  98776  98052  97389  96564  94548

[46]  94428  93556  93085  89464  84706

new <- as.numeric(y_mod)              #this command converts string to numeric


No comments:

Post a Comment