jonny jeep
jonny jeep

Reputation: 417

create a dataframe with one row and multiple columns

I would to create a data frame with 12 columns and one row. I have this data:

Tissues <-
 [1] "ova"                      "testes"                   "optic_lobe"               "retina"                   "suckers"                 
 [6] "subesophageal_brain"      "Supraesophageal_brain"    "skin"                     "stage15tissues"           "axial_nerve_corde"       
[11] "posterior_salivary_gland" "viscera"                 
 reads <-
 [1] 22444744 30671024 22648756 24002307 26438055 48999389 60168243 30172728 26812536 23359754 24038817 25570689

I would to create this:

       tissue1 tissue2 tissue3 tissue4 tissue5 tissue6 etc..
reads       1       2       3        4      5        6  

I've tried:

df <- data.frame(matrix(ncol = 12, nrow = 1))
x <- TESSUTI
colnames(df) <- x

but how can I add the reads to each columns?

anyone could help me?

Upvotes: 2

Views: 4826

Answers (3)

DSGym
DSGym

Reputation: 2867

df <- setNames(data.frame(t(c(22444744, 30671024, 22648756, 24002307, 26438055, 48999389, 60168243, 30172728, 26812536, 23359754, 24038817, 25570689))),
         c("ova", "testes", "optic_lobe", "retina","suckers" , "subesophageal_brain", "Supraesophageal_brain",
           "skin", "stage15tissues", "axial_nerve_corde", "posterior_salivary_gland","viscera"))
rownames(df) <- "read"
df

Most of it in 1 line, which results in:

          ova   testes optic_lobe   retina  suckers subesophageal_brain Supraesophageal_brain     skin stage15tissues axial_nerve_corde posterior_salivary_gland  viscera
read 22444744 30671024   22648756 24002307 26438055            48999389              60168243 30172728       26812536          23359754                 24038817 25570689

Upvotes: 1

Johan Rosa
Johan Rosa

Reputation: 3152

You can create a data frame with two vector and spread as columns one of them using spread() from tidyr

library(tidyr)

# data
Tissues <- c( "ova", "testes", "optic_lobe", "retina", "suckers",
   "subesophageal_brain","Supraesophageal_brain","skin",
   "stage15tissues", "axial_nerve_corde", "posterior_salivary_gland", "viscera")                 

reads <-c(22444744, 30671024, 22648756, 24002307, 26438055, 48999389,
          60168243, 30172728, 26812536, 23359754, 24038817, 25570689)


df <- *emphasized text*data.frame(Tissues, reads) %>%
  spread(Tissues, reads)

Upvotes: 1

Matt Dzievit
Matt Dzievit

Reputation: 527

Helpful to have a reproducible example that we can easily copy and paste.

Here is a solution:

##Your input data
tissues <- letters[1:12]
reads <- rnorm(n = 12,mean = 250000,sd = 1000)

##Put the reads into one row of a matrix, then convert to dataframe
data <- data.frame(matrix(nrow = 1,data = reads))

##set the colnames to match your tissue, and the reads row to have a reads name
colnames(data) <- tissues
rownames(data) <- 'reads'

print(data)
           a      b      c      d      e      f      g      h      i      j      k      l
reads 250813 250376 250178 251138 251033 249945 250165 248150 251168 251193 249658 250621

Upvotes: 3

Related Questions