How can I fix the output of a cat() function in R involving a comma which prints it out with too many spaces?

Viewed 51

I am trying to print out a concatenation of a subset of two different objects to which lists have been assigned to match a specific desired format for the output of a Backward Elimination Stepwise Regression I am fitting to each of 47,500 different datasets (each with 501 rows & 31 columns) which are all in the same file folder. Each csv dataset in this file folder is named in the following format:

#-#-#-#, e.g. 0-5-1-1, 0-5-1-2, etc.

That desired output is the name of the csv file containing each individual dataset and the factors selected by the BE Stepwise regression fitted to that dataset, i.e. in the following manner where the numbers after the comma following the filename each indicate a factor selected by the BE Stepwise Regression run on that dataset: #-#-#-#, # # # # # # # # #; for example

0-3-1-2, 1, 2, 9, 12, 0-5-1-8, 7

Here is my code:

# these 2 lines together create a simple character list of 
# all the file names in the file folder of datasets you created
directory_path2 <- "~/DAEN_698/sample_obs2"
file_list2 <- list.files(path = directory_path2, full.names = TRUE, recursive = TRUE)
head(file_list2, n = 1)
# Console - [1] "C:/Users/Spencer/Documents/DAEN_698/sample_obs2/0-5-1-1.csv"

# Separate out just the characters in the csv file names themselves
nchar(head(file_list2, n = 1))    # Console says [1] 59
# 0-5-1-1 is 6 characters & .csv is 4, so it'll be 49:55
DS_name_list2 = stri_sub(file_list2, 49, 55)
head(DS_name_list2, n = 2)        # Console says [1] "0-5-1-1" "0-5-1-2"

# assign the full model to an object
all_regressors_model1 <- lm(formula = Y ~ ., data = data_2_1)

set.seed(50)     # for replicability
BE_fit1 <- step(all_regressors_model1, direction = 'backward', 
       scope = formula(all_regressors_model1), trace = 0)
# separating out just the coefficients selected by the 1st BE Regression 
names(BE_fit1[["coefficients"]][-1])
# Console - [1] "X1"  "X2"  "X3"  "X4"  "X5"  "X9"  "X11" "X13" "X14" "X22" "X27"

comma <- ", "
cat(DS_name_list2[1], comma, names(BE_fit1[['coefficients']][-1]))

But the Console prints out:

0-5-1-1 , X1 X2 X3 X4 X5 X9 X11 X13 X14 X22 X27,

not

0-5-1-1, 1, 2, 3, 4, 5, 9, 11, 13, 14, 22, 27

as required by my collaborator on the paper we are working on or even:

0-5-1-1, X1 X2 X3 X4 X5 X9 X11 X13 X14 X22 X27,

i.e. no space before the 1st comma & only 1 space after it before X1 which I would find acceptable for now.

I have tried the answer offered to a similar question posed in this post: enter link description here But when I used the answer to the stackoverflow question in the link, namely:

cat(paste0(DS_name_list2[1], comma, names(BE_fit1[['coefficients']][-1])))

I got the following back in the Console:

0-5-1-1, X1 0-5-1-1, X2 0-5-1-1, X3 0-5-1-1, X4 0-5-1-1, X5 0-5-1-1, X9 0-5-1-1, X11 0-5-1-1, X13 0-5-1-1, X14 0-5-1-1, X22 0-5-1-1, X27

Is there a simple fix for this?

p.s. Sorry for including so much detail, I just wanted to make absolutely sure that the answers that come back will actually work immediately, I have a deadline approaching!

1 Answers

Mock up of solution

# representing names(BE_fit1[['coefficients']][-1]
myStrings<-c("X1", "X2", "X3");  

# representing DS_name_list2[1]
FirstPart<-"0-5-1-1"  

# remove all "X" characters
SecondPart<-stringr::str_replace_all(myStrings, "X", "") 

# concatenate the components to the console
cat(FirstPart, SecondPart, sep=", ")  

0-5-1-1, 1, 2, 3

Related