nik
nik

Reputation: 2584

how to add names to each object of a list

I have a list like below

d1 <- data.frame(y1=c(1,2,3),y2=c(4,5,6))
d2 <- data.frame(y1=c(3,2,1),y2=c(6,5,4))
my.list <- list(d1, d2)
str(my.list)
List of 2
 $ :'data.frame':   3 obs. of  2 variables:
  ..$ y1: num [1:3] 1 2 3
  ..$ y2: num [1:3] 4 5 6
 $ :'data.frame':   3 obs. of  2 variables:
  ..$ y1: num [1:3] 3 2 1
  ..$ y2: num [1:3] 6 5 4

what i want to have is to add names as i wish to each object of this list for example

 $myFirst :'data.frame':    3 obs. of  2 variables:
  ..$ y1: num [1:3] 1 2 3
  ..$ y2: num [1:3] 4 5 6
 $mySecond :'data.frame':   3 obs. of  2 variables:
  ..$ y1: num [1:3] 3 2 1
  ..$ y2: num [1:3] 6 5 4

I tired to do it by

myNam<-c("myFirst","mySecond")
names(myNam) <- sapply(my.list,paste)

where is the problem ?

A new data which the function does not work on it

df<- structure(list(A = structure(list(breaks = c(-10, -9, -8, -7, 
-6, -5, -4, -3, -2, -1, 0, 1, 2, 3, 4), counts = c(1L, 0L, 1L, 
5L, 9L, 38L, 56L, 105L, 529L, 2858L, 17L, 2L, 0L, 2L), density = c(0.000276014352746343, 
0, 0.000276014352746343, 0.00138007176373171, 0.00248412917471709, 
0.010488545404361, 0.0154568037537952, 0.028981507038366, 0.146011592602815, 
0.788849020149048, 0.00469224399668783, 0.000552028705492686, 
0, 0.000552028705492686), mids = c(-9.5, -8.5, -7.5, -6.5, -5.5, 
-4.5, -3.5, -2.5, -1.5, -0.5, 0.5, 1.5, 2.5, 3.5), xname = "x", 
    equidist = TRUE), .Names = c("breaks", "counts", "density", 
"mids", "xname", "equidist"), class = "histogram"), B = structure(list(
    breaks = c(-7, -6, -5, -4, -3, -2, -1, 0), counts = c(2L, 
    0L, 6L, 2L, 2L, 1L, 3L), density = c(0.125, 0, 0.375, 0.125, 
    0.125, 0.0625, 0.1875), mids = c(-6.5, -5.5, -4.5, -3.5, 
    -2.5, -1.5, -0.5), xname = "x", equidist = TRUE), .Names = c("breaks", 
"counts", "density", "mids", "xname", "equidist"), class = "histogram"), 
    C = structure(list(breaks = c(-7, -6, -5, -4, -3, -2, -1, 
    0, 1), counts = c(2L, 2L, 4L, 5L, 14L, 22L, 110L, 3L), density = c(0.0123456790123457, 
    0.0123456790123457, 0.0246913580246914, 0.0308641975308642, 
    0.0864197530864197, 0.135802469135802, 0.679012345679012, 
    0.0185185185185185), mids = c(-6.5, -5.5, -4.5, -3.5, -2.5, 
    -1.5, -0.5, 0.5), xname = "x", equidist = TRUE), .Names = c("breaks", 
    "counts", "density", "mids", "xname", "equidist"), class = "histogram")), .Names = c("A", 
"B", "C"))

Upvotes: 1

Views: 71

Answers (1)

akrun
akrun

Reputation: 887128

We need to assign the 'myNam' to the names of my.list

names(my.list) <- myNam
str(my.list)
#List of 2
#$ myFirst :'data.frame':       3 obs. of  2 variables:
# ..$ y1: num [1:3] 1 2 3
# ..$ y2: num [1:3] 4 5 6
#$ mySecond:'data.frame':       3 obs. of  2 variables:
#  ..$ y1: num [1:3] 3 2 1
#  ..$ y2: num [1:3] 6 5 4

or with setNames

setNames(my.list, myNam)

The OP's code

sapply(my.list,paste)

is looping through the list elements and pasteing the elements of the columns to a single string.

Upvotes: 2

Related Questions