R 中的字符串操作:如何使用 cat(...,sep=" ") 打印但省略最后一个 space?
String manipulation in R: How to print with cat(...,sep=" ") but omit the last space?
我正在使用 R 打印 MPLUS 命令。我必须指定许多变量之间的协方差。因此,我必须创建一个集合的所有子集。在 MPLUS 中,它必须像这样指定(如果我们考虑 4 个变量 female、age、edu migrant 的列表):
female WITH age edu migrant;
age WITH edu migrant;
edu WITH migrant;
代表 4 个变量集的大小为 2 的所有子集(female WITH age;female WITH edu;female WITH migrant;age WITH edu;age WITH migrant;edu WITH migrant;)。
为了获得可以直接复制到 MPLUS 中的输出,我使用了命令 cat()。不幸的是,我无法得到上面显示的输出,只能得到这个输出(注意分号):
female WITH age edu migrant ;
age WITH edu migrant ;
edu WITH migrant ;
我经常使用 paste、cat 和 print。但是要么我在行尾的分号之前得到一个 space 的输出(就像上面直接的那样),要么我得到这个:
female WITH ageedumigrant;
age WITH edumigrant;
edu WITH migrant;
所以我的问题基本上是:如何省略 cat(...,sep=" ") 命令中的最后一个 space?
我的小函数是这样的:
library(stringr)
vars_b <- "female age edu migrant"
covstructure <- function(x, cov = TRUE, var = TRUE, width = 80) {
# decode variable list into a vector, e.g. x[1] = "female" #
x <- gsub("(?<=[\s])\s*|^\s+|\s+$", "", x, perl=TRUE)
x <- unlist(strsplit(x, " "))
# covariance structure (the part of interest here) #
if(cov==TRUE) {
# all combinations #
result <- combn(x,2)
# get subsets into the MPLUS format: #
# female WITH age edu migrant; #
# age WITH edu migrant; #
# edu WITH migrant; #
for(i in 1:(length(x)-1)) {
# indices of the combinations that include the i-th variable #
ind <- which(result==x[i])
# print variable WITH its combinations #
# here is my problem: #
cat(result[which.min(ind)], "WITH", result[ind+1], ";", fill=width)
# create new combinations without the combinations of preceding variables, i.e. 1 to i #
if(i < length(x)-1) { result <- combn(x[-c(1:i)],2) }
}
}
# variance structure (not of interest) #
if(var==TRUE) {
cat(x, "", sep="; ", fill=width)
}
}
covstructure(vars_b, cov=TRUE, var=FALSE)
我希望我能足够仔细地列出问题(这完全是关于 R 字符串操作)并提前非常感谢你。
本
以下代码对您有用吗?
x <- c("female", "WITH", "age", "edu", "migrant")
cat(cat(x), ";", sep="")
这可能是一个很好的方法 -
x <- c("female", "WITH", "age", "edu", "migrant")
y<-do.call(paste, c(as.list(x), sep=" "))
k<-paste(y,";",sep="")
现在字符串 k 将成为您想要的字符串 -
[1] "female WITH age edu migrant;"
我正在使用 R 打印 MPLUS 命令。我必须指定许多变量之间的协方差。因此,我必须创建一个集合的所有子集。在 MPLUS 中,它必须像这样指定(如果我们考虑 4 个变量 female、age、edu migrant 的列表):
female WITH age edu migrant;
age WITH edu migrant;
edu WITH migrant;
代表 4 个变量集的大小为 2 的所有子集(female WITH age;female WITH edu;female WITH migrant;age WITH edu;age WITH migrant;edu WITH migrant;)。
为了获得可以直接复制到 MPLUS 中的输出,我使用了命令 cat()。不幸的是,我无法得到上面显示的输出,只能得到这个输出(注意分号):
female WITH age edu migrant ;
age WITH edu migrant ;
edu WITH migrant ;
我经常使用 paste、cat 和 print。但是要么我在行尾的分号之前得到一个 space 的输出(就像上面直接的那样),要么我得到这个:
female WITH ageedumigrant;
age WITH edumigrant;
edu WITH migrant;
所以我的问题基本上是:如何省略 cat(...,sep=" ") 命令中的最后一个 space?
我的小函数是这样的:
library(stringr)
vars_b <- "female age edu migrant"
covstructure <- function(x, cov = TRUE, var = TRUE, width = 80) {
# decode variable list into a vector, e.g. x[1] = "female" #
x <- gsub("(?<=[\s])\s*|^\s+|\s+$", "", x, perl=TRUE)
x <- unlist(strsplit(x, " "))
# covariance structure (the part of interest here) #
if(cov==TRUE) {
# all combinations #
result <- combn(x,2)
# get subsets into the MPLUS format: #
# female WITH age edu migrant; #
# age WITH edu migrant; #
# edu WITH migrant; #
for(i in 1:(length(x)-1)) {
# indices of the combinations that include the i-th variable #
ind <- which(result==x[i])
# print variable WITH its combinations #
# here is my problem: #
cat(result[which.min(ind)], "WITH", result[ind+1], ";", fill=width)
# create new combinations without the combinations of preceding variables, i.e. 1 to i #
if(i < length(x)-1) { result <- combn(x[-c(1:i)],2) }
}
}
# variance structure (not of interest) #
if(var==TRUE) {
cat(x, "", sep="; ", fill=width)
}
}
covstructure(vars_b, cov=TRUE, var=FALSE)
我希望我能足够仔细地列出问题(这完全是关于 R 字符串操作)并提前非常感谢你。
本
以下代码对您有用吗?
x <- c("female", "WITH", "age", "edu", "migrant")
cat(cat(x), ";", sep="")
这可能是一个很好的方法 -
x <- c("female", "WITH", "age", "edu", "migrant")
y<-do.call(paste, c(as.list(x), sep=" "))
k<-paste(y,";",sep="")
现在字符串 k 将成为您想要的字符串 -
[1] "female WITH age edu migrant;"