用gsub()修复R中的字符向量的问题
我有一个数据集,其标题有“”、”和“…”符号。我尝试使用用gsub()修复R中的字符向量的问题,r,vector,R,Vector,我有一个数据集,其标题有“”、”和“…”符号。我尝试使用r中的gsub函数删除文本变量,但符号仍然保留 下面是函数: #Load dataset Smallstore1 <- read.csv("/Users/scdavis6/Documents/Work/TowerData/TowerData/Smallclient1.csv", na.strings = "", head = TRUE) #Convert csv to data.frame fr
r
中的gsub
函数删除文本变量,但符号仍然保留
下面是函数:
#Load dataset
Smallstore1 <- read.csv("/Users/scdavis6/Documents/Work/TowerData/TowerData/Smallclient1.csv",
na.strings = "", head = TRUE)
#Convert csv to data.frame
frame <- as.data.frame(Smallstore1, stringsAsFactors = FALSE)
#Clean up titles of data.frame
gsub("_", "...", ".", Smallstore1)
如果我能提供更多信息,请告诉我
编辑:我尝试了评论中建议的两种解决方案,但没有修复字符向量。
> names(Smallstore1) <- gsub("_|\\.\\.\\.|\\." , "" , names(Smallstore1))
> gsub("_|\\.\\.\\.|\\." , "" , names(Smallstore1))
如果要清理变量名,需要执行以下操作:
names(Smallstore1) <- gsub("_|\\.\\.\\.|\\." , "", names(Smallstore1))
names(Smallstore1)您可以使用“[.]”
转义来捕获所有名称。您放入“[]”
中的任何标点符号都将受到保护,不需要转义。在gsub
中,只有一个
将删除字符串中的所有点(sub
将只删除第一个出现的点)
您还可以使用“[()())]”
进行匹配,也可以删除括号。但是我喜欢它们,因为它使代码更容易阅读
我只是注意到您的代码中有gsub(““,“…”,“,”,Smallstore1)
。最好阅读一下?gsub
您不正确地使用了gsub
。请查看帮助以了解应指定的参数。我敢打赌你的意思是:gsub(“\u124\\\\.\\.\\.\\.\\.\”,“,Smallstore1)
需要处理名称(Smallstore1)
,而不是整个dfrm。@Bonded你说得对!RE:您的编辑,很可能是因为使用第一个解决方案,您用user\u id
列中的数据重写了数据框的所有名称…,再次读入数据并使用@Reed的答案中的解决方案。@SimonO'Hanlon我在设置stringsAsFactors=FALSE后再次读取数据并得到正确的结果。谢谢需要将名称
围绕第三个参数包装到gsub
。从评论中抄袭答案而不加归因可能会让你在本网站上名声扫地。这显然是最节省的答案;我是独立提出的。我听说,诬告是另一种获得坏名声的方式。你独立提出的,包括我犯的完全相同的错误。做得好。如果你相信……@Reed,我试过你的建议,但得到了与西蒙相同的答案。请看我的编辑。
> names(Smallstore1)
[1] "c(12945, 12947, 12990, 13160, 13195, 13286, 13464, 13501, 13532, 13613, 13660, 13668, 13719, 13776, 13821, 13834, 13858, 13915, 13953, 13977, 14078, 14133, 14157, 14174, 14181, 14187, 14191, 14204, 14276, 14334, 14382, 14439, 14473, 14497, 14507, 14538, 14548, 14555, 14565, 14595, 14620, 14705, 14731, 14752, 14810, 14824, 14827, 14864, 14875, 14983, 14994, 15048, 15096, 15147, 15194, 15234, 15269, 15334, 15381, 15405, 15449, 15453, 15462, 15625, 15646, 15666, 15687, 15708, 15731, 15782, 15823, 15914, \n15935, 16014, 16065, 16095, 16173, 16269, 16289, 16339, 16374, 16408, 16445, 16465, 16527, 16547, 16561, 16581, 16609, 16646, 16677, 16768, 16779, 16792, 16830, 16839, 16849, 17064, 17071, 17149, 17159, 17261, 17346, 17377, 17427, 17428, 17448, 17652, 17737, 17765, 17768, 17808, 17897, 17907, 17910, 17961, 17999, 18122, 18159, 18175, 18397, 18434, 18583, 18635, 18683, 18685, 18713, 18754, 18825, 18839, 18900, 18913, 19040, 19063, 19091, 19144, 19199, 19233, 19308, 19315, 19335, 19366, 19417, 19533, \n19539, 19546, 19553, 19604, 19658, 19669, 19689, 19767, 19791, 19825, 19869, 19998, 20032, 20046, 20107, 20168, 20175, 20287, 20457, 20464, 20481, 20590, 20634, 20647, 20651, 20753, 20783, 20794, 20872, 20967, 21001, 21046, 21110, 21114, 21117, 21191, 21199, 21246, 21253, 21327, 21358, 21409, 21412, 21420, 21480, 21494, 21497, 21508, 21522, 21633, 21637, 21675, 21684, 21698, 21729, 21831, 21847, 21868, 21916, 21950, 21984, 22018, 22021, 22092, 22242, 22249, 22259, 22323, 22364, 22453, 22582, 22606, \n22616, 22619, 22623, 22629, 22630, 22633, 22698, 22776, 22793, 22827, 22891, 22905, 22973, 23010, 23014, 23038, 23052, 23106, 23163, 23173, 23191, 23377, 23388, 23401, 23409, 23466, 23520, 23568, 23670, 23677, 23762, 23823, 23847, 23908, 23925, 23936, 23939, 24017, 24177, 24289, 24313, 24316, 24340, 24395, 24401, 24421, 24480, 24548, 24602, 24718, 24731, 24778, 24833, 24840, 24843, 24877, 24908, 24969, 24990, 25061, 25064, 25224, 25254, 25258, 25268, 25275, 25296, 25367, 25418, 25438, 25445, 25496, \n25553, 25588, 25653, 25707, 25751, 25945, 25989, 26002, 26023, 26057, 26132, 26166)"
names(Smallstore1) <- gsub("_|\\.\\.\\.|\\." , "", names(Smallstore1))
> txt
# [1] "user_id" "email" "Age"
# [4] "Gender" "Household.Income" "Marital.Status"
# [7] "Presence.of.Children" "Home.Owner.Status" "Home.Market.Value"
# [10] "Occupation" "Education" "Zip.Code"
# [13] "High.Net.Worth" "Length.of.Residence" "Arts...Crafts"
# [16] "Automotive"
> gsub("[.]|[_]", " ", txt)
# [1] "user id" "email" "Age"
# [4] "Gender" "Household Income" "Marital Status"
# [7] "Presence of Children" "Home Owner Status" "Home Market Value"
# [10] "Occupation" "Education" "Zip Code"
# [13] "High Net Worth" "Length of Residence" "Arts Crafts"
# [16] "Automotive"