Adjusting the printing format for the given variables in R

In summary, the code provided uses the data.table library to calculate the values of a and b based on the given data. It then uses a for loop to print the values of a and b in a formatted string. However, the output may have some formatting issues. To fix this, the paste function can be used with the collapse keyword to join the values of a and b together.
  • #1
Arman777
Insights Author
Gold Member
2,168
193
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
  • #2
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.
 
  • #3
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
  • #4
Try paste0( "(" , var , ")" )
 
  • #5
It does not produce anything.. its really weird
 
  • #6
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 , ")" ))
 
  • #7
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)
 
  • #8
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)
 
  • #9
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
 

Related to Adjusting the printing format for the given variables in R

1. How can I adjust the printing format for a specific variable in R?

To adjust the printing format for a specific variable in R, you can use the "format()" function. This function allows you to specify the format you want the variable to be displayed in, such as scientific notation or currency format.

2. Can I adjust the printing format for multiple variables at once in R?

Yes, you can adjust the printing format for multiple variables at once in R by using the "cat()" function. This function allows you to concatenate multiple variables and specify the format for each one.

3. How do I change the decimal places in the printing format for a variable in R?

To change the number of decimal places in the printing format for a variable in R, you can use the "options(digits = )" function. Simply specify the number of digits you want to display after the decimal point.

4. Is it possible to adjust the printing format for a variable based on its data type in R?

Yes, you can adjust the printing format for a variable based on its data type in R by using the "dplyr" package. This package allows you to manipulate data frames and specify the format for each variable based on its data type.

5. How can I save the adjusted printing format for a variable in R?

To save the adjusted printing format for a variable in R, you can use the "saveRDS()" function. This function allows you to save the variable with its adjusted format as a separate file, which can be loaded and used in future R sessions.

Back
Top