malligator
malligator

Reputation: 129

Apply parentheses around elements of R dataframe

Apologies for the basic question but I am very new to R. I have a two row dataframe that I will export to Latex and am trying to amend the second (currently numeric) row so that it retains 4 decimal places and is encased in parentheses.

Is this possible?

Current dataframe: df:

  col1   |   col2   |   col3
-------------------------------
 0.0553     0.1354     0.1403
 0.0000     0.0956     0.9110

Desired dataframe:

  col1   |   col2   |   col3
-------------------------------
 0.0553     0.1354     0.1403
(0.0000)   (0.0956)   (0.9110)

Upvotes: 5

Views: 5862

Answers (1)

akrun
akrun

Reputation: 887691

Try

df1[2,] <- paste0("(", format(unlist(df1[2,])),")")
df1
#     col1     col2     col3
#1   0.0553   0.1354   0.1403
#2  (0.0000) (0.0956) (0.9110)

data

df1 <- structure(list(col1 = c(0.0553, 0), col2 = c(0.1354, 0.0956), 
col3 = c(0.1403, 0.911)), .Names = c("col1", "col2", "col3"
 ), class = "data.frame", row.names = c(NA, -2L))

Upvotes: 9

Related Questions