Tom
Tom

Reputation: 2341

Make R print a Stata command

I have data as follows:

old <- c("a", "b b", "c")
new <- c("1", "2", "3")
combined <- setNames(new, old)

I would like to be able to convert this in the following format:

a 1 b b 2 c 3

Most importantly, I want to be able to print the output to console (or copy it from a cell), so that I can copy it. The reason is that I need to run something in Stata. The Stata syntax is as follows:

coeflabels(mpg Milage rep78 "Repair Record")

Stata: coeflabels(name label [...]) specifies labels for the coefficients. Specify names and labels in pairs and, if necessary, enclose labels in double quotes, e.g. coeflabels(mpg Milage rep78 "Repair Record".

If at all possible I would like to be able to reproduce produce the following output:

a 1 "b b" 2 c 3

Any ideas?

Upvotes: 0

Views: 58

Answers (1)

GKi
GKi

Reputation: 39657

You can use rbind on the names and combined and bring it to a vector with c. Hope the additional " are not a problem.

c(rbind(names(combined), combined))
#[1] "a"   "1"   "b b" "2"   "c"   "3"  

Or using cat without [1] and without ":

cat(rbind(names(combined), combined), "\n")
#a 1 b b 2 c 3

or adding " where there is a space:

tt <- rbind(names(combined), combined)
i <- grep(" ", tt)
tt[i] <- paste0("\"", tt[i], "\"")
cat(tt, "\n")
#a 1 "b b" 2 c 3

Upvotes: 1

Related Questions