我有一个数据框:
Agent Info
A {"id": 0, "value": 235, "hours": 40} {}
B {"id": 1, "value": 771, "hours": 64} {}
C HttpResponse(202 Accepted,List(Server: SAP Application) {}
如您所见,在Info列中每个值的末尾都有空格和大括号{}。如何摆脱我的数据框?我真的没有在这里写什么样的正则表达式
答案 0 :(得分:0)
假设您只想删除结尾的花括号,甚至不需要使用正则表达式:
代码:
df[, 2] <- sapply(df[, 2], function(a){
unlist(strsplit(a, " {}", fixed = T))[1]
})
# c.1..2..3. c.a..b..c.
#1 1 {'id': 0, 'value': 235, 'hours': 40}
#2 2 {'id': 1, 'value': 771, 'hours': 64}
#3 3 HttpResponse(202 Accepted,List(Server: SAP Application)
数据:
a <- "{'id': 0, 'value': 235, 'hours': 40} {}"
b <- "{'id': 1, 'value': 771, 'hours': 64} {}"
c <- "HttpResponse(202 Accepted,List(Server: SAP Application) {}"
df <- data.frame(c(1, 2, 3), c(a, b, c))
# c.1..2..3. c.a..b..c.
#1 1 {'id': 0, 'value': 235, 'hours': 40} {}
#2 2 {'id': 1, 'value': 771, 'hours': 64} {}
#3 3 HttpResponse(202 Accepted,List(Server: SAP Application) {}
答案 1 :(得分:0)
使用sub
,您可以执行以下操作:
df$Info <- sub('\\s\\{\\}$', '', df$Info)
df
# Agent Info
#1 A {id: 0, value: 235, hours: 40}
#2 B {id: 1, value: 771, hours: 64}
#3 C HttpResponse(202 Accepted,List(Server: SAP Application)