0

I created DT putting the values in a table:

a <- 2
b <- 20
c <- 0.3
DT <- data.table(a, b, c)

let's assume that I have only the DT generated. How can from DT generate a, b, c (single values)

Thanks and best regards, SH

2 Answers2

0

With $ or indicating the "cell" in the table. There are many ways to do this.

library(data.table)
a <- 2
b <- 20
c <- 0.3
DT <- data.table(a, b, c)
DT
> DT
   a  b   c
1: 2 20 0.3
x <- DT$a
y <- DT$b
z <- DT$c
DT2 <- data.table(x, y, z)
DT2
> DT2
   x  y   z
1: 2 20 0.3
xa <- DT[1,1]
yb <- DT[1,2]
zc <- DT[1,3]
DT3 <- data.table(xa, yb, zc)
DT3
> DT3
   a  b   c
1: 2 20 0.3
bbiasi
  • 1,354
  • 1
  • 10
  • 25
  • Sorry for the unclarity in my question, I meant to have it done not variable by variable but for all the variables. If DT is 1 by n, I do not want to write n time the subsetting. – highbury_85 May 18 '19 at 11:49
  • 1
    Given this comment and some of the comments below, I'm a bit curious about what data structure you are expecting as output. If your DT is 3 columns and 3 rows are you expecting the output to be 9 variables? 3 lists (or vectors) each representing a row? If you are expecting the output to be 9 variables (in my example), it's not clear why you would need to do this. Why not just extract the values from the DT individually when you want to consume them? – hank_044 May 18 '19 at 13:35
  • DT is a wide table (1 by n), n listed paramters: a, b, c, d.... What I expect is to extract from the data table all these parameters in R studio as values and call them as values without subsetting each time. I want to have all of them outside of the data.table, I do not want to subset them one by one doing DT$a, DT$b but I want to get all of them as values where a get value 2, b get value 20 and so on for all the n parameters. Is now a bit more clear? @bbiasi can you just subset a value one by one in a data table (basic r now-how) or can you somehow also do it? Thanks for the effort anyway. – highbury_85 May 19 '19 at 09:40
0

This is basic R knowledge. I suggest you do a basic course (try install.packages(swirl), or datacamp) and pay attention to the basic subsetting parts.

bob1
  • 378
  • 2
  • 12
  • I do not mean 1 by 1, let's assume that DT is 1*50, I do not want to write it 50 times. I did not expect 3 lines of code to do such operation but one. – highbury_85 May 18 '19 at 11:47