How to convert data.frame to list?

Asked

Viewed 1,613 times

3

I loaded my data using

taxadados<-read.csv2("ComponentesPrincipaisTeste.csv",header=TRUE)


TAXA         Taxa.1   Taxa.2  Taxa.3 Taxa.4  Taxa.5 Taxa.6   Taxa.7 Taxa.8
09/04/2013  32.7188   8.8350 13.0662 9.0114  8.7003 8.9924  76.7003 9.2542
10/04/2013  376.7003  8.8170 67.0762 8.9842  8.9924 9.0395   8.9924 9.2247
11/04/2013  67.9924   8.8589 13.0656 9.0357 14.3199 8.9912 667.3199 9.2815

They came, obviously, as Data.Frame.

How do I convert them to "list format"?

1 answer

3


You can use the function as.list.

For example:

df <- read.table(text = "TAXA  Taxa.1  Taxa.2  Taxa.3 Taxa.4  Taxa.5 Taxa.6  Taxa.7 Taxa.8
09/04/2013  32.7188  8.8350 13.0662 9.0114  8.7003 8.9924  76.7003 9.2542
10/04/2013  376.7003  8.8170 67.0762 8.9842  8.9924 9.0395  8.9924 9.2247
11/04/2013  67.9924  8.8589 13.0656 9.0357 14.3199 8.9912 667.3199 9.2815", header = TRUE)

str(df)
'data.frame':   3 obs. of  9 variables:
 $ TAXA  : Factor w/ 3 levels "09/04/2013","10/04/2013",..: 1 2 3
 $ Taxa.1: num  32.7 376.7 68
 $ Taxa.2: num  8.84 8.82 8.86
 $ Taxa.3: num  13.1 67.1 13.1
 $ Taxa.4: num  9.01 8.98 9.04
 $ Taxa.5: num  8.7 8.99 14.32
 $ Taxa.6: num  8.99 9.04 8.99
 $ Taxa.7: num  76.7 8.99 667.32
 $ Taxa.8: num  9.25 9.22 9.28

Making a list:

lista <- as.list(df)
str(lista)
List of 9
 $ TAXA  : Factor w/ 3 levels "09/04/2013","10/04/2013",..: 1 2 3
 $ Taxa.1: num [1:3] 32.7 376.7 68
 $ Taxa.2: num [1:3] 8.84 8.82 8.86
 $ Taxa.3: num [1:3] 13.1 67.1 13.1
 $ Taxa.4: num [1:3] 9.01 8.98 9.04
 $ Taxa.5: num [1:3] 8.7 8.99 14.32
 $ Taxa.6: num [1:3] 8.99 9.04 8.99
 $ Taxa.7: num [1:3] 76.7 8.99 667.32
 $ Taxa.8: num [1:3] 9.25 9.22 9.28

In general, in the R, when you want to convert one object into another you will use a function of type as.objeto.

It is worth remembering, however, that a data.frame is already a list, only with more attributes. So when doing as.list you’re just taking out these attributes (like row.names).

is.list(df)
TRUE
  • I am trying to calculate a Cubic Spline by the function estim_cs(bonddata, group, matrange="all", rse=TRUE) where "bonddata" is in the format "List". I was able to turn it into Lis but when I calculate the response function is: no applicable method for 'estim_cs' Applied to an Object of class "list".

  • Where can I be missing?

  • 1

    @Diogobastos, you’d better ask another question then by putting exactly the package you’re using etc.

  • 1

    @Diogobastos As Carlos explained, a data.frame is already a list. I don’t remember seeing any case where you need to do this kind of transformation, "normal" lists are useful when the elements are of different sizes or depths.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.