我相信以下代码可以满足问题的要求。它用于rep获取重复,然后将paste它们放在一起。
s <- strsplit(substring(variables, 2), "\\.")
sapply(s, function(x){
vec <- c("x", "y", "z")[seq_along(x)]
x <- as.integer(x)
y <- rep(vec, rev(x))
paste(y, collapse = "")
})
# [1] "x" "y" "z" "xx" "xy" "yy" "xz" "yz" "zz" "xxx"
#[11] "xxy" "xyy" "yyy" "xxz" "xyz" "yyz" "xzz" "yzz" "zzz"
编辑。
以下函数尝试回答评论中的对话框。它返回一个带有字符串及其度数的 data.frame。然后是排序的问题degree/chr。
changeVariable <- function(x, chr = c("x", "y", "z")){
s <- strsplit(substring(x, 2), "\\.")
y <- lapply(s, function(.x){
vec <- chr[seq_along(.x)]
.x <- as.integer(.x)
.y <- rep(vec, rev(.x))
list(chr = paste(.y, collapse = ""),
degree = sum(.x)
)
})
res <- do.call(rbind.data.frame, y)
row.names(res) <- NULL
res
}
res <- changeVariable(variables)
res[order(res$degree, res$chr), ]
编辑 2。
结果粘贴有上标:
changeVariable2 <- function(x){
s <- strsplit(substring(x, 2), "\\.")
y <- lapply(s, function(.x){
vec <- c("x", "y", "z")[seq_along(.x)]
.x <- rev(as.integer(.x))
.y <- vec[.x != 0]
.x <- .x[.x != 0]
list(chr = paste0(.y, "^", .x, collapse = " "),
degree = sum(.x)
)
})
res <- do.call(rbind.data.frame, y)
row.names(res) <- NULL
res
}