Adjusting the printing format for the given variables in R

Click For Summary
The discussion focuses on formatting output in R for a set of calculated points derived from data. The user initially attempts to format the output using the `cat` function but encounters issues with unwanted spaces and formatting. Suggestions include using `paste0` for concatenation and `trimws` to remove trailing spaces. However, the user finds that `paste0` does not print as expected. The conversation shifts to using `paste` with the `collapse` argument, which effectively combines elements into a single string without extra spaces. The final solution involves using `cat` with `paste` and the `collapse` option to achieve the desired output format of points without additional spaces. Additionally, participants suggest using online compilers for testing R code, providing a link to a useful resource.
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 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 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 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 FactChecker, jedishrfu and Arman777
  • #15
that works .. thanks