Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/77.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/cmake/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
R 如何反转字符串的分隔部分_R_Hierarchy_Concat - Fatal编程技术网

R 如何反转字符串的分隔部分

R 如何反转字符串的分隔部分,r,hierarchy,concat,R,Hierarchy,Concat,我试图反转在数据帧中找到的层次名称的部分,以便可以使用反向路径的字符串。以下是我所做的: flip <- function(x) { # My attempted function str <- str_split(x,":",simplify=TRUE) paste(str[length(str):1], collapse = ":") } Data <- data.frame(

我试图反转在数据帧中找到的层次名称的部分,以便可以使用反向路径的字符串。以下是我所做的:

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
flip 2五:六:七:八八:四:七:三:六:二:五:一
#我在找什么
#>X Xflip
#>一:二:三:四:三:二:一
#>二五:六:七:八:七:六:五

谢谢大家!

我会拆分、翻转,然后连接。但使用mutate可能有一种更简单的方法:)


flipper或使用
stringr::str\u split\u fixed

df$Xflip <- apply(stringr::str_split_fixed(df$X, ":", 4), 1, function(x) 
    paste0(rev(x), collapse = ":"))
#                     X                Xflip
#1   one:two:three:four   four:three:two:one
#2 five:six:seven:eight eight:seven:six:five
您可以使用
转换
执行相同的操作:

transform(df, Xflip = sapply(stringr::str_split(X, ":"), function(x) 
    paste0(rev(x), collapse = ":")))

如果
X
包含不同数量的
:“
分隔条目,则
stringr::stru split
方法也会起作用。

对不起,上面的示例中有一些输入错误。我将编辑我的示例。感谢您的回复。我不清楚这是否是完全重复的,因为我需要反转分隔字符串的部分,而不是字符串中的每个字符。我编辑了标题以反映这种差异。我注意到在“如何在R中反转字符串”一文中,执行了strsplit,但使用NULL作为分隔符-不知道为什么。感谢您的帮助。很明显,关于R编程,我还有很多东西要学。你激励我多“挖”一点。别担心@a.Maffei;很高兴我能帮忙。如果答案是有用的,请考虑投票。
df$Xflip <- apply(stringr::str_split_fixed(df$X, ":", 4), 1, function(x) 
    paste0(rev(x), collapse = ":"))
#                     X                Xflip
#1   one:two:three:four   four:three:two:one
#2 five:six:seven:eight eight:seven:six:five
df$Xflip <- sapply(stringr::str_split(df$X, ":"), function(x) 
    paste0(rev(x), collapse = ":"));
transform(df, Xflip = sapply(stringr::str_split(X, ":"), function(x) 
    paste0(rev(x), collapse = ":")))