Search code examples
rdataframelistvector

How to reshape the dimensions in R


This is my data in R, it is a list:

$class
    1     2     3     4     5     6     7     8     9    10    11    12    13    14    15    16    17    18    19    20    21    22    23    24    25    26    27    28    29    30 
<=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K  >50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K 
   31    32    33 

I wish to have it in the below format:

 [1] <=50K <=50K <=50K >50K  <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K >50K  >50K  <=50K <=50K >50K  <=50K <=50K >50K  <=50K >50K  >50K  <=50K <=50K <=50K <=50K <=50K <=50K
  [30] <=50K >50K  <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K >50K  <=50K <=50K <=50K <=50K <=50K <=50K >50K  >50K  <=50K <=50K <=50K <=50K <=50K <=50K <=50K
  [59] <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K <=50K >50K  <=50K <=50K <=50K <=50K >50K  <=50K <=50K <=50K <=50K <=50K <=50K <=50K >50K  >50K  <=50K <=50K <=50K <=50K >50K 
  [88] <=50K >50K  >50K  >50K  <=50K <=50K <=50K <=50K <=50K

I tried doing it in the below way, but I am unable to achieve the correct format, how do I reshape it?

a <- as.numeric(adult_test_pred[[1]])
a

The output of a is:

   [1] 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 2 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
  [88] 1 1 1 1 1 1 1 1 1 1 1 2 1

Any help is really appreciated.


Solution

  • It is a named vector, we need unname

    unname(adult_test_pred[[1]])
    

    If it is a named factor, just use as.character, which will also remove the names

    as.character(adult_test_pred[[1]])
    

    e.g.

    > v1 <- c("1" = "sam", "2" = "hello")
    > as.character(v1)
    [1] "sam"   "hello"