Adjusting the printing format for the given variables in R

Click For Summary

Discussion Overview

The discussion revolves around formatting output in R, specifically how to adjust the printing format of variables to achieve a desired output style. Participants explore various methods and functions to format the output correctly.

Discussion Character

  • Technical explanation
  • Debate/contested
  • Mathematical reasoning

Main Points Raised

  • One participant shares their initial code and desired output format, indicating issues with the current output style.
  • Another participant suggests using the `sep` argument in the `cat` function to control spacing, but notes that it may leave an unwanted final space.
  • A different participant proposes using `paste0` to concatenate strings, but others point out that it does not print the output as expected.
  • There are repeated suggestions to use `cat(paste0(...))` to format the output, with some participants expressing confusion about the results.
  • One participant mentions the use of an online compiler to test the code, prompting others to consider this approach.
  • A later reply introduces the `collapse` argument in `paste`, suggesting it could be a solution to format the output correctly.
  • Another participant confirms that the suggested solution works for their needs.

Areas of Agreement / Disagreement

Participants express various methods to format the output, but there is no consensus on a single best approach until the final solution is confirmed to work by one participant.

Contextual Notes

Some participants express uncertainty about the behavior of certain functions in R, and there are mentions of potential issues with whitespace in the output. The discussion reflects a range of experiences with R programming.

Who May Find This Useful

This discussion may be useful for R users looking to format output in a specific way, particularly those dealing with data visualization or presentation of results in a clean format.

Arman777
Insights Author
Gold Member
Messages
2,163
Reaction score
191
I have data like

Code:
 -418 -26066 -539 -33810
    -763 -47745 207 12395
    -701 14732 473 -8748
    862 -19061 744 -16347
    680 59377 -637 -53885
    -720 35840 -486 23906
    -147 3505 762 -20129
    677 -53800 849 -67388
    -690 42730 995 -63425
    203 -4108 620 -11614
    93 -6381 26 -1423
    -230 -6255 135 3600
    498 -8020 341 -5665
    855 -35988 306 -12381
    69 -4017 -329 17475

and my code is

Code:
  library(data.table)
    
    x1 <- Problem10_data$V1
    y1 <- Problem10_data$V2
    x2 <- Problem10_data$V3
    y2 <- Problem10_data$V4
    
    a = (y2 - y1) / (x2 - x1)
    b = (x2*y1 - x1*y2) / (x2 - x1)
    
    points <- transpose(data.frame(a = a, b = b))
    
    for (var in points){
      cat('(', var, ')', sep = " ")
    }

my output is

Code:
( 64 686 )( 62 -439 )( -20 712 )...

but what I want is

Code:
(64 686) (62 -439) (-20 712)

so how can I format my output..?
 
Technology news on Phys.org
It looks to me as though you should try to reduce the sep string to nothing and put the spacing that you want in your own strings. Like:
cat( "(", var, ") ", sep = "")
This will put a final space at the end of the string that you might not like. If you want to remove it, try using the trimws function after the loop.
 
FactChecker said:
It looks to me as though you should try to reduce the sep string to nothing and put the spacing that you want in your own strings. Like:
cat( "(", var, ") ", sep = "")
This will put a final space at the end of the string that you might not like. If you want to remove it, try using the trimws function after the loop.
sadly that does not work..
 
  • Sad
Likes   Reactions: FactChecker
Try paste0( "(" , var , ")" )
 
It does not produce anything.. its really weird
 
Sorry - it's been a few years since I used R. I think paste0 concatenates strings but doesn't print them. You could, therefore, try cat(paste0 "(" , var , ")" ))
 
FactChecker said:
It looks to me as though you should try to reduce the sep string to nothing and put the spacing that you want in your own strings. Like:
cat( "(", var, ") ", sep = "")
This will put a final space at the end of the string that you might not like. If you want to remove it, try using the trimws function after the loop.
When you type sep = "" it also removes the whitespace between the bars so it prints

(64686) (62-439) (-20712)
 
Ibix said:
Sorry - it's been a few years since I used R. I think paste0 concatenates strings but doesn't print them. You could, therefore, try cat(paste0 "(" , var , ")" ))
It prints

(64) (686)(62) (-439)(-20)
 
you guys could use online compiler to run the code at least.. ?
 
  • Haha
Likes   Reactions: jedishrfu
  • #10
In that case, cat(paste0("(",paste(var),")"))
Arman777 said:
you guys could use online compiler to run the code at least.. ?
Do you have a link?
 
  • #11
Arman777 said:
you guys could use online compiler to run the code at least.. ?
Trying. No success yet.
 
  • #13
FactChecker said:
Trying. No success yet.
Ibix said:
In that case, cat(paste0("(",paste(var),")"))

Do you have a link?
Code:
a <- c(64, 686)
b <- c(62, -439)

points <- data.frame(a = a, b = b)

for (var in points){
      cat('(', var, ')', sep = " ")
    }

Run this on

compile R online (rextester.com)
 
  • Like
Likes   Reactions: Ibix
  • #14
Code:
a <- c(64, 686)
b <- c(62, -439)

points <- data.frame(a = a, b = b)

for (var in points){
      cat('(', paste( var , collapse=" " ) , ') ' ,sep="" )
}
The collapse keyword is similar to sep, but it takes paste's arguments as lists and collapses them instead of joining them element-wise.
 
  • Like
Likes   Reactions: FactChecker, jedishrfu and Arman777
  • #15
that works .. thanks