Adjusting the printing format for the given variables in R

Join the discussion
Ask a follow-up here, or get your own question answered by working scientists, mathematicians and engineers — people, not an autocomplete.
Real named experts · corrections over time · the nuance an AI answer skips
14 replies · 1K views
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..?
 
Physics 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
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
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
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