【发布时间】:2018-03-06 22:42:22
【问题描述】:
我正在尝试反转在数据框中找到的分层名称的部分,以便我可以使用反向路径的字符串。这是我所做的:
flip <- function(x) { # My attempted function
str <- str_split(x,":",simplify=TRUE)
paste(str[length(str):1], collapse = ":")
}
Data <- data.frame( # The data
X = c("one:two:three:four","five:six:seven:eight")
)
mutate(Data, # My attempt & result
Xflip = flip(X)
)
#> X Xflip
#>1 one:two:three:four eight:four:seven:three:six:two:five:one
#>2 five:six:seven:eight eight:four:seven:three:six:two:five:one
# What I am looking for
#> X Xflip
#>1 one:two:three:four four:three:two:one
#>2 five:six:seven:eight eight:seven:six:five
谢谢!
【问题讨论】:
-
我很抱歉——我上面的例子中有一些错别字。我将编辑我的示例。感谢您到目前为止的回复。
-
我不清楚这是完全重复的,因为我需要反转分隔字符串的部分而不是字符串中的每个字符。我编辑了标题以反映这种差异。我注意到在“如何在 R 中反转字符串”文章中执行了 strsplit,但使用 NULL 作为分隔符 - 不知道为什么。