粘贴多个列表元素以创建单个字符串

时间:2019-06-15 06:16:58

标签: r list paste

我目前有一个笨拙的解决方案,用于将文本和列表元素粘贴在一起以形成单个字符串。一些空白必须留在其中,而其他一些则必须除去。

我的代码当前确实产生了预期的结果:

“文本1具有空格(x + 60)(x + 60)-(y + 30)(y + 30)-(z-20)*(z-20)文本2具有空格a1Text3“

但这不是那么雄辩,我想知道是否有更好的方法来达到相同的结果?

x <- seq(-60,60, length.out = 5)
y <- seq(-30,10, length.out = 5)
z <- seq(20,60, length.out = 5)

area <- c("a1","a2","a3","a4", "a5")

newlist <- list(x,y,z,area)

Text1 <- "Text 1 has spaces"
Text2 <- "Text2 has spaces"
Text3 <- "Text3"

formula <- paste("(x-", newlist[[1]],")*","(x-", newlist[[1]],")", "-",
                 "(y-", newlist[[2]],")*","(y-", newlist[[2]],")","-",
                 "(z-", newlist[[3]],")*","(z-", newlist[[3]],")")

formula <- gsub(" ", "", formula)
formula <- gsub("--", "+", formula)

newname <- paste(newlist[[4]],Text3)
newname <- gsub(" ", "", newname) 

result <- paste(Text1,formula,Text2,newname)
result
[1] "Text 1 has spaces (x+60)*(x+60)-(y+30)*(y+30)-(z-20)*(z-20) Text2 has spaces a1Text3"

[2] "Text 1 has spaces (x+30)*(x+30)-(y+20)*(y+20)-(z-30)*(z-30) Text2 has spaces a2Text3"

[3] "Text 1 has spaces (x-0)*(x-0)-(y+10)*(y+10)-(z-40)*(z-40) Text2 has spaces a3Text3"  

[4] "Text 1 has spaces (x-30)*(x-30)-(y-0)*(y-0)-(z-50)*(z-50) Text2 has spaces a4Text3"  

[5] "Text 1 has spaces (x-60)*(x-60)-(y-10)*(y-10)-(z-60)*(z-60) Text2 has spaces a5Text3"

1 个答案:

答案 0 :(得分:0)

您可以通过将所有内容放入paste0然后gsub绑定一次来消除几行:

lis <- list(x = seq(-60,60, length.out = 5),
            y = seq(-30,10, length.out = 5),
            z = seq(20,60, length.out = 5),
            area = c("a1","a2","a3","a4", "a5"))

result <- paste0("Text1 has spaces (x-", lis[[1]], ")*(x-", lis[[1]],
                 ")-(y-", lis[[2]], ")*", "(y-", lis[[2]], ")-(z-", lis[[3]], 
                 ")*(z-", lis[[3]], ") Text2 has spaces ", lis[[4]], "Text3"
              )
result <- gsub("--", "+", result)

#### OUTPUT ####

[1] "Text1 has spaces (x+60)*(x+60)-(y+30)*(y+30)-(z-20)*(z-20) Text2 has spaces a1Text3"
[2] "Text1 has spaces (x+30)*(x+30)-(y+20)*(y+20)-(z-30)*(z-30) Text2 has spaces a2Text3"
[3] "Text1 has spaces (x-0)*(x-0)-(y+10)*(y+10)-(z-40)*(z-40) Text2 has spaces a3Text3"  
[4] "Text1 has spaces (x-30)*(x-30)-(y-0)*(y-0)-(z-50)*(z-50) Text2 has spaces a4Text3"  
[5] "Text1 has spaces (x-60)*(x-60)-(y-10)*(y-10)-(z-60)*(z-60) Text2 has spaces a5Text3"

或者,您也可以使用glue软件包:

library(glue)

result <- glue("Text1 has spaces (x-{lis[[1]]})*(x-{lis[[1]]})-(y-{lis[[2]]})*",
               "(y-{lis[[2]]})-(z-{lis[[3]]})*(z-{lis[[3]]}) Text2 has spaces ",
               "{lis[[4]]}Text3",
              )
result <- gsub("--", "+", result)

#### OUTPUT ####

Text1 has spaces (x+60)*(x+60)-(y+30)*(y+30)-(z-20)*(z-20) Text2 has spaces a1Text3
Text1 has spaces (x+30)*(x+30)-(y+20)*(y+20)-(z-30)*(z-30) Text2 has spaces a2Text3
Text1 has spaces (x-0)*(x-0)-(y+10)*(y+10)-(z-40)*(z-40) Text2 has spaces a3Text3
Text1 has spaces (x-30)*(x-30)-(y-0)*(y-0)-(z-50)*(z-50) Text2 has spaces a4Text3
Text1 has spaces (x-60)*(x-60)-(y-10)*(y-10)-(z-60)*(z-60) Text2 has spaces a5Text3

请注意,将返回一个“胶水”对象。您可以使用unclassas.character将其转换为“字符”类型。