Trim 关闭改变 R 中的最后一个特殊字符

Trim Off Varying Last Special Characters in R

下面是一个 gsub 方法,用于从数据帧中删除正斜杠。希望为具有不同列数的 data.frame 找到更通用的解决方案。

helloToday <- data.frame(a = c("hello", "hello", "hello"), 
                 b = c("world","","world"),
                 c = c("","","today"))

helloToday
#      a     b     c
# 1 hello world      
# 2 hello            
# 3 hello world today  


# Returns the vector 
helloToday <- apply(helloToday, 1, function(x){ paste0("/", paste(x, collapse = "/")) })
# [1] "/hello/world/"      "/hello//"           "/hello/world/today"

# But I would like the trailing symbols to be trimmed off
# [1] "/hello/world"      "/hello"           "/hello/world/today"


gsub("\/$", "", gsub("\/$", "", helloToday))
# "/hello/world/"      "/hello//"           "/hello/world/today"

helloToday <- gsub("\//$", "", helloToday)
helloToday <- gsub("\/$", "", helloToday)
# "/hello/world/"      "/hello//"           "/hello/world/today"

是否有允许不同列数的解决方案,其中“/”或“//”甚至“//////////”?

+ 是 "one or more" 的正则表达式修饰符,因此 "/+$" 将匹配字符串末尾的任意数量的 /

gsub("/+$", "", helloToday)

Ann 替代正则表达式的方法是在开始时以不同的方式构建它:

apply(helloToday, 1, function(x) do.call(file.path, as.list(x[!x %in% ''])))


## [1] "hello/world"       "hello"             "hello/world/today"

如果需要前导斜线:

apply(helloToday, 1, function(x) do.call(file.path, as.list(c('', x[!x %in% '']))))