Home > Software engineering >  Convert a data frame to into row vector or long vector
Convert a data frame to into row vector or long vector

Time:06-29

I'm not sure what is the exact data type that is but when I see class It shows character and numeric.

My intended data what I want to use small subset is this

a <- dput(head(Age))
structure(list(Blast_Percentage = c(82L, 83L, 91L, 72L, 68L, 
88L)), row.names = c("TCGA-AB-2856", "TCGA-AB-2849", "TCGA-AB-2971", 
"TCGA-AB-2930", "TCGA-AB-2891", "TCGA-AB-2872"), class = "data.frame")

Second data frame

b <- dput(head(Sample_info))
structure(list(patient = structure(c(35L, 32L, 118L, 93L, 64L, 
47L), .Label = c("TCGA-AB-2805", "TCGA-AB-2806", "TCGA-AB-2808", 
"TCGA-AB-2810", "TCGA-AB-2811", "TCGA-AB-2812", "TCGA-AB-2813", 
"TCGA-AB-2814", "TCGA-AB-2815", "TCGA-AB-2817", "TCGA-AB-2818", 
"TCGA-AB-2819", "TCGA-AB-2820", "TCGA-AB-2821", "TCGA-AB-2822", 
"TCGA-AB-2823", "TCGA-AB-2825", "TCGA-AB-2826", "TCGA-AB-2828", 
"TCGA-AB-2830", "TCGA-AB-2834", "TCGA-AB-2835", "TCGA-AB-2836", 
"TCGA-AB-2839", "TCGA-AB-2840", "TCGA-AB-2841", "TCGA-AB-2842", 
"TCGA-AB-2844", "TCGA-AB-2845", "TCGA-AB-2846", "TCGA-AB-2847", 
"TCGA-AB-2849", "TCGA-AB-2851", "TCGA-AB-2853", "TCGA-AB-2856", 
"TCGA-AB-2858", "TCGA-AB-2859", "TCGA-AB-2861", "TCGA-AB-2862", 
"TCGA-AB-2863", "TCGA-AB-2865", "TCGA-AB-2866", "TCGA-AB-2867", 
"TCGA-AB-2869", "TCGA-AB-2870", "TCGA-AB-2871", "TCGA-AB-2872", 
"TCGA-AB-2873", "TCGA-AB-2874", "TCGA-AB-2875", "TCGA-AB-2876", 
"TCGA-AB-2877", "TCGA-AB-2878", "TCGA-AB-2880", "TCGA-AB-2881", 
"TCGA-AB-2882", "TCGA-AB-2883", "TCGA-AB-2884", "TCGA-AB-2885", 
"TCGA-AB-2886", "TCGA-AB-2888", "TCGA-AB-2889", "TCGA-AB-2890", 
"TCGA-AB-2891", "TCGA-AB-2892", "TCGA-AB-2893", "TCGA-AB-2894", 
"TCGA-AB-2895", "TCGA-AB-2896", "TCGA-AB-2897", "TCGA-AB-2898", 
"TCGA-AB-2899", "TCGA-AB-2900", "TCGA-AB-2901", "TCGA-AB-2908", 
"TCGA-AB-2910", "TCGA-AB-2911", "TCGA-AB-2912", "TCGA-AB-2913", 
"TCGA-AB-2914", "TCGA-AB-2915", "TCGA-AB-2916", "TCGA-AB-2917", 
"TCGA-AB-2918", "TCGA-AB-2919", "TCGA-AB-2920", "TCGA-AB-2921", 
"TCGA-AB-2924", "TCGA-AB-2925", "TCGA-AB-2927", "TCGA-AB-2928", 
"TCGA-AB-2929", "TCGA-AB-2930", "TCGA-AB-2931", "TCGA-AB-2932", 
"TCGA-AB-2933", "TCGA-AB-2934", "TCGA-AB-2935", "TCGA-AB-2936", 
"TCGA-AB-2937", "TCGA-AB-2939", "TCGA-AB-2940", "TCGA-AB-2942", 
"TCGA-AB-2943", "TCGA-AB-2944", "TCGA-AB-2946", "TCGA-AB-2948", 
"TCGA-AB-2949", "TCGA-AB-2950", "TCGA-AB-2952", "TCGA-AB-2955", 
"TCGA-AB-2956", "TCGA-AB-2959", "TCGA-AB-2963", "TCGA-AB-2965", 
"TCGA-AB-2966", "TCGA-AB-2970", "TCGA-AB-2971", "TCGA-AB-2973", 
"TCGA-AB-2975", "TCGA-AB-2976", "TCGA-AB-2977", "TCGA-AB-2979", 
"TCGA-AB-2980", "TCGA-AB-2981", "TCGA-AB-2982", "TCGA-AB-2983", 
"TCGA-AB-2984", "TCGA-AB-2986", "TCGA-AB-2987", "TCGA-AB-2988", 
"TCGA-AB-2990", "TCGA-AB-2991", "TCGA-AB-2992", "TCGA-AB-2994", 
"TCGA-AB-2995", "TCGA-AB-2996", "TCGA-AB-2998", "TCGA-AB-2999", 
"TCGA-AB-3000", "TCGA-AB-3001", "TCGA-AB-3002", "TCGA-AB-3007", 
"TCGA-AB-3008", "TCGA-AB-3009", "TCGA-AB-3011", "TCGA-AB-3012"
), class = "factor"), FAB = structure(c(5L, 1L, 5L, 3L, 2L, 4L
), .Label = c("M0", "M1", "M2", "M3", "M4", "M5"), class = "factor")), row.names = c(NA, 
6L), class = "data.frame")

The way I want to turn this data frame is into this

> age
 s1  s2  s3  s4  s5  s6  s7  s8  s9 s10 s11 s12 s13 s14 s15 s16 s17 s18 s19 s20 s21 s22 s23 s24 s25 s26 s27 s28 s29 s30 s31 s32 s33 s34 s35 s36 s37 s38 
  2   2   5  11  20  32  61  93  93 144 232 303 649 654 904   2   2   5  11  20  32  61  93  93 144 232 303 649 654 904 904   2   2   5  11  20  32  61 
s39 s40 s41 s42 s43 s44 s45 s46 s47 s48 s49 s50 s51 s52 s53 s54 s55 s56 s57 s58 s59 s60 s61 s62 s63 
 93  93 144 232 303 649 654 904 904   2   2   5  11  20  32  61  93  93 144 232 303 649 654 904 904 
> class(age)
[1] "numeric"

and the other one would be for the second data frame it should look like this

samp_tissue
      s1       s2       s3       s4       s5       s6       s7       s8       s9      s10      s11      s12      s13      s14      s15      s16      s17 
"Cortex" "Cortex" "Cortex" "Cortex" "Cortex" "Cortex" "Cortex" "Cortex" "Cortex" "Cortex" "Cortex" "Cortex" "Cortex" "Cortex" "Cortex"   "Lung"   "Lung" 
     s18      s19      s20      s21      s22      s23      s24      s25      s26      s27      s28      s29      s30      s31      s32      s33      s34 
  "Lung"   "Lung"   "Lung"   "Lung"   "Lung"   "Lung"   "Lung"   "Lung"   "Lung"   "Lung"   "Lung"   "Lung"   "Lung"   "Lung"  "Liver"  "Liver"  "Liver" 
     s35      s36      s37      s38      s39      s40      s41      s42      s43      s44      s45      s46      s47      s48      s49      s50      s51 
 "Liver"  "Liver"  "Liver"  "Liver"  "Liver"  "Liver"  "Liver"  "Liver"  "Liver"  "Liver"  "Liver"  "Liver"  "Liver" "Muscle" "Muscle" "Muscle" "Muscle" 
     s52      s53      s54      s55      s56      s57      s58      s59      s60      s61      s62      s63 
"Muscle" "Muscle" "Muscle" "Muscle" "Muscle" "Muscle" "Muscle" "Muscle" "Muscle" "Muscle" "Muscle" "Muscle" 

How to convert them into above type I would like to know.

I tried to search but Im not sure about the right query to find what Im looking for

Possible solution what I got from this post

whatyouwant <- setNames( as.numeric(Age$Blast_Percentage),as.character(Age$patient))
class(whatyouwant)
what <- setNames( as.character(Sample_info$FAB),as.character(Age$patient))
class(what)
what

CodePudding user response:

Your expected outcome is not clear to me (i.e. using your sample data, what should the final outcome be?), but perhaps you're looking for table() (my guess for "a") or deframe() from the tibble package:

library(tibble)

a <- structure(list(Blast_Percentage = c(82L, 83L, 91L, 72L, 68L, 
                                    88L)), row.names = c("TCGA-AB-2856", "TCGA-AB-2849", "TCGA-AB-2971", 
                                                         "TCGA-AB-2930", "TCGA-AB-2891", "TCGA-AB-2872"), class = "data.frame")
b <- structure(list(patient = structure(c(35L, 32L, 118L, 93L, 64L, 
                                     47L), .Label = c("TCGA-AB-2805", "TCGA-AB-2806", "TCGA-AB-2808", 
                                                      "TCGA-AB-2810", "TCGA-AB-2811", "TCGA-AB-2812", "TCGA-AB-2813", 
                                                      "TCGA-AB-2814", "TCGA-AB-2815", "TCGA-AB-2817", "TCGA-AB-2818", 
                                                      "TCGA-AB-2819", "TCGA-AB-2820", "TCGA-AB-2821", "TCGA-AB-2822", 
                                                      "TCGA-AB-2823", "TCGA-AB-2825", "TCGA-AB-2826", "TCGA-AB-2828", 
                                                      "TCGA-AB-2830", "TCGA-AB-2834", "TCGA-AB-2835", "TCGA-AB-2836", 
                                                      "TCGA-AB-2839", "TCGA-AB-2840", "TCGA-AB-2841", "TCGA-AB-2842", 
                                                      "TCGA-AB-2844", "TCGA-AB-2845", "TCGA-AB-2846", "TCGA-AB-2847", 
                                                      "TCGA-AB-2849", "TCGA-AB-2851", "TCGA-AB-2853", "TCGA-AB-2856", 
                                                      "TCGA-AB-2858", "TCGA-AB-2859", "TCGA-AB-2861", "TCGA-AB-2862", 
                                                      "TCGA-AB-2863", "TCGA-AB-2865", "TCGA-AB-2866", "TCGA-AB-2867", 
                                                      "TCGA-AB-2869", "TCGA-AB-2870", "TCGA-AB-2871", "TCGA-AB-2872", 
                                                      "TCGA-AB-2873", "TCGA-AB-2874", "TCGA-AB-2875", "TCGA-AB-2876", 
                                                      "TCGA-AB-2877", "TCGA-AB-2878", "TCGA-AB-2880", "TCGA-AB-2881", 
                                                      "TCGA-AB-2882", "TCGA-AB-2883", "TCGA-AB-2884", "TCGA-AB-2885", 
                                                      "TCGA-AB-2886", "TCGA-AB-2888", "TCGA-AB-2889", "TCGA-AB-2890", 
                                                      "TCGA-AB-2891", "TCGA-AB-2892", "TCGA-AB-2893", "TCGA-AB-2894", 
                                                      "TCGA-AB-2895", "TCGA-AB-2896", "TCGA-AB-2897", "TCGA-AB-2898", 
                                                      "TCGA-AB-2899", "TCGA-AB-2900", "TCGA-AB-2901", "TCGA-AB-2908", 
                                                      "TCGA-AB-2910", "TCGA-AB-2911", "TCGA-AB-2912", "TCGA-AB-2913", 
                                                      "TCGA-AB-2914", "TCGA-AB-2915", "TCGA-AB-2916", "TCGA-AB-2917", 
                                                      "TCGA-AB-2918", "TCGA-AB-2919", "TCGA-AB-2920", "TCGA-AB-2921", 
                                                      "TCGA-AB-2924", "TCGA-AB-2925", "TCGA-AB-2927", "TCGA-AB-2928", 
                                                      "TCGA-AB-2929", "TCGA-AB-2930", "TCGA-AB-2931", "TCGA-AB-2932", 
                                                      "TCGA-AB-2933", "TCGA-AB-2934", "TCGA-AB-2935", "TCGA-AB-2936", 
                                                      "TCGA-AB-2937", "TCGA-AB-2939", "TCGA-AB-2940", "TCGA-AB-2942", 
                                                      "TCGA-AB-2943", "TCGA-AB-2944", "TCGA-AB-2946", "TCGA-AB-2948", 
                                                      "TCGA-AB-2949", "TCGA-AB-2950", "TCGA-AB-2952", "TCGA-AB-2955", 
                                                      "TCGA-AB-2956", "TCGA-AB-2959", "TCGA-AB-2963", "TCGA-AB-2965", 
                                                      "TCGA-AB-2966", "TCGA-AB-2970", "TCGA-AB-2971", "TCGA-AB-2973", 
                                                      "TCGA-AB-2975", "TCGA-AB-2976", "TCGA-AB-2977", "TCGA-AB-2979", 
                                                      "TCGA-AB-2980", "TCGA-AB-2981", "TCGA-AB-2982", "TCGA-AB-2983", 
                                                      "TCGA-AB-2984", "TCGA-AB-2986", "TCGA-AB-2987", "TCGA-AB-2988", 
                                                      "TCGA-AB-2990", "TCGA-AB-2991", "TCGA-AB-2992", "TCGA-AB-2994", 
                                                      "TCGA-AB-2995", "TCGA-AB-2996", "TCGA-AB-2998", "TCGA-AB-2999", 
                                                      "TCGA-AB-3000", "TCGA-AB-3001", "TCGA-AB-3002", "TCGA-AB-3007", 
                                                      "TCGA-AB-3008", "TCGA-AB-3009", "TCGA-AB-3011", "TCGA-AB-3012"
                                     ), class = "factor"), FAB = structure(c(5L, 1L, 5L, 3L, 2L, 4L
                                     ), .Label = c("M0", "M1", "M2", "M3", "M4", "M5"), class = "factor")), row.names = c(NA, 
                                                                                                                          6L), class = "data.frame")

table(a)
#> a
#> 68 72 82 83 88 91 
#>  1  1  1  1  1  1
deframe(b)
#> TCGA-AB-2856 TCGA-AB-2849 TCGA-AB-2971 TCGA-AB-2930 TCGA-AB-2891 TCGA-AB-2872 
#>           M4           M0           M4           M2           M1           M3 
#> Levels: M0 M1 M2 M3 M4 M5

Created on 2022-06-29 by the reprex package (v2.0.1)

Or maybe this?

library(tidyverse)

a %>%
  rownames_to_column("patient") %>%
  left_join(b)
#> Joining, by = "patient"
#>        patient Blast_Percentage FAB
#> 1 TCGA-AB-2856               82  M4
#> 2 TCGA-AB-2849               83  M0
#> 3 TCGA-AB-2971               91  M4
#> 4 TCGA-AB-2930               72  M2
#> 5 TCGA-AB-2891               68  M1
#> 6 TCGA-AB-2872               88  M3

Created on 2022-06-29 by the reprex package (v2.0.1)

  •  Tags:  
  • r
  • Related