简体   繁体   中英

How to remove RowNames and Column Names in Matrix in R

I have an output as

       VAR FREQ
  1     A   2 
  2     R   3
  3     B   4

I want to show result as

        A  2
        R  3
        B  4

Which means that i want to remove Column Names and Serial Number

The query i am using is

data.frame(sort(table(substr(read.csv('extra/name.csv')$value, 1, 1)),decreasing =TRUE))

Kindly help with some pointers

We can set dimnames to NULL assuming it is a matrix as mentioned in the post

dimnames(m1) <- NULL

If we need to print , this can be done with cat as @Gregor showed

d1 <- as.data.frame(m1)
cat(do.call(paste, c(d1, collapse='\n')), '\n')
#A 2
#R 3
#B 4 

data

m1 <- structure(c("A", "R", "B", "2", "3", "4"), .Dim = 3:2, .Dimnames = list(
c("1", "2", "3"), c("VAR", "FREQ")))

cat can be used to print results to the console without the niceties added by print .

dd = read.table(text = "      VAR FREQ
  1     A   2 
  2     R   3
  3     B   4", header = T, stringsAsFactors = FALSE)

dd
#   VAR FREQ
# 1   A    2
# 2   R    3
# 3   B    4

for(i in 1:nrow(dd)) cat(unlist(dd[i, ]), "\n")
# A 2 
# R 3 
# B 4 

Note that if your data has factor s, you will need to convert them to character to print like this. No guarantees are made about how columns of other classes will print (eg, Date class). You also lose the justification and rounding done automatically by print .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM