R Currency() changing only one Column

134 views Asked by At

Hey im new to R and wondering how i can with currency() of the package formattable format only 1 column.

matrix <- currency(matrix) working

currency(matrix[,2]) working

BUT

matrix[,2] <- currency(matrix[,2])

OR

matrix[1,2] <- currency(matrix[1,2])

doesn't change anything in matrix

edit: My Problem in a nutshell

1

There are 1 answers

3
akrun On BEST ANSWER

The issue is that formattable adds some attributes and matrix cannot have those attributes. An option is to convert to data.frame

library(formattable)
df1 <- as.data.frame(matrix)
df1[,2] <- currency(df1[,2])
df1
#  V1     V2  V3
#1  1 $10.00 100
#2  2 $20.00 200
#3  3 $30.00 300
#4  4 $40.00 400

data

matrix <- cbind(1:4, c(10, 20, 30, 40), c(100, 200, 300, 400))