Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Pipe operator using in R

As I am just starting to learn the R, I want to re write the below code into the pipe operator way. however, the setting rowname and colname blocked me. I will be grateful if some one can help me, which is highly appreciated!

The original code is detailed as below,

data_3 <- c(692, 5, 0, 629, 44, 9)
table_3 <- matrix(data_3, ncol=3, byrow = TRUE)
colnames(table_3) <- c("Improved", "Hospitalized", "Death")
rownames(table_3) <- c("Treated", "Placebo")
table_3 <- as.table(table_3)

chisq.test(table_3)
fisher.test(table_3)

however, I got into trouble in the setting the colname and rowname when I tried to use the pipe operator.

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

c(692, 5, 0, 629, 44, 9) %>% 
  matrix(ncol = 3) %>% 
  colnames <- c("Improved", "Hospitalized", "Death")

I will be grateful if anyone could help me in this pipe operator using.

your support is highly appreciated!

Best regards,

Charles

>Solution :

You want to do:

`colnames<-`(c("Improved", "Hospitalized", "Death"))

Note the backticks and no spaces as colnames<- is actually a function.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading