0

Hi everybody I am working with list of data frames in R and it is awesome but I am having a little problem. I have the next list of data frames. Real extension of the list got more than 20 data frames with 1000 observations and 20 columns. Here I put the structure:

my.list

$a1
  alfa beta
1    1    2
2    1    3
3    1    4
4    1    5
5    1    6
6    1    7

$a2
   alfa beta
1     1    2
2     1    3
3     1    4
4     1    5
5     1    6
6     1    7
7     2    1
8     2    5
9     2    9
10    2   13
11    2   17
12    2   21
13    2   25
14    2   29
15    2   33

$a3
   alfa beta
1     1    2
2     1    3
3     1    4
4     1    5
5     1    6
6     1    7
7     2    1
8     2    5
9     2    9
10    2   13
11    2   17
12    2   21
13    2   25
14    2   29
15    2   33
16    3   11
17    3    2
18    3   -7
19    3  -16
20    3  -25

$a4
   alfa beta
1     1    2
2     1    3
3     1    4
4     1    5
5     1    6
6     1    7
7     2    1
8     2    5
9     2    9
10    2   13
11    2   17
12    2   21
13    2   25
14    2   29
15    2   33
16    3   11
17    3    2
18    3   -7
19    3  -16
20    3  -25
21    5    1
22    1    1
23    5    1

$a5
   alfa beta
1     2   13
2     2   17
3     2   21
4     2   25
5     2   29
6     2   33
7     3   11
8     3    2
9     3   -7
10    3  -16
11    3  -25
12    5    1
13    1    1
14    5    1

Where the elements of my.list are a1,a2,a3,a5,a5. I was using functions to try to create something like this in the elements of my.list:

$a1
  alfa beta  a1
1    1    2  1
2    1    3  1
3    1    4  1
4    1    5  1
5    1    6  1
6    1    7  1

$a2
   alfa beta  a2
1     1    2  2
2     1    3  2
3     1    4  2
4     1    5  2
5     1    6  2
6     1    7  2
7     2    1  2
8     2    5  2
9     2    9  2
10    2   13  2
11    2   17  2
12    2   21  2
13    2   25  2
14    2   29  2
15    2   33  2

$a3
   alfa beta  a3
1     1    2  3
2     1    3  3
3     1    4  3
4     1    5  3
5     1    6  3
6     1    7  3
7     2    1  3
8     2    5  3
9     2    9  3
10    2   13  3
11    2   17  3
12    2   21  3
13    2   25  3
14    2   29  3
15    2   33  3
16    3   11  3
17    3    2  3
18    3   -7  3
19    3  -16  3
20    3  -25  3

$a4
   alfa beta  a4
1     1    2  4
2     1    3  4
3     1    4  4
4     1    5  4
5     1    6  4
6     1    7  4
7     2    1  4
8     2    5  4
9     2    9  4
10    2   13  4
11    2   17  4
12    2   21  4
13    2   25  4
14    2   29  4
15    2   33  4
16    3   11  4
17    3    2  4
18    3   -7  4
19    3  -16  4
20    3  -25  4
21    5    1  4
22    1    1  4
23    5    1  4

$a5
   alfa beta  a5
1     2   13  5
2     2   17  5
3     2   21  5
4     2   25  5
5     2   29  5
6     2   33  5
7     3   11  5
8     3    2  5
9     3   -7  5
10    3  -16  5
11    3  -25  5
12    5    1  5
13    1    1  5
14    5    1  5

I want to compute for each data frame in my.list a new variable whose name is equal to the name of data frame (a1,a2,a3,a4,a5) and its value is equal to the position of the data frame in my.list (my.list[1]=a1,my.list[2]=a2,my.list[3]=a3,my.list[4]=a4,my.list[5]=a5), where the numbers in brackets show the position or data frames. I was using llply form plyr package but the functions doesn't work. I wait someone can help me with this

5 Answers 5

4
lapply(seq(my.list), function(x) "[[<-"(my.list[[x]], paste0("a", x), value = x))

will do the trick.

The function "[[<-"(x, y, value = z) is similar to x[[y]] <- z but does not modify x. Instead, the modified version is returned.

Sign up to request clarification or add additional context in comments.

2 Comments

could you explain what "[[<-" is doing"?
@noah I added an explanation to my answer.
1
ll <- list(a1 = data.frame(alfa = 1:3), a2 = data.frame(alfa = 1:3))

ll[] <- lapply(seq_along(ll), function(x){
  setnames(cbind(ll[[x]], x), c(names(ll[[x]]), names(ll)[x]))
  })

# $a1
#   alfa a1
# 1    1  1
# 2    2  1
# 3    3  1
# 
# $a2
#   alfa a2
# 1    1  2
# 2    2  2
# 3    3  2

Update with a mapply alternative

mapply(function(x, y){
  setNames(cbind(x, y),  c(names(ll[[y]]), names(ll)[y]))
},
       ll, seq_along(ll),
       SIMPLIFY = FALSE)

If the name of the new variable isn't an issue, the mapply code can be simplified:

mapply(cbind, ll, a = seq_along(ll), SIMPLIFY = FALSE)

Comments

0
kk <- list(a1 = data.frame(data1 = 4:9), a2 = data.frame(data2 = 1:6))
myvar<-as.list(names(kk))
myval<-as.list(1:length(kk))
jj<-Map(function(x,y,z) cbind(x,setNames(cbind(x,z)[2],y)),kk,myvar,myval)

  > jj
$a1
  data1 a1
1     4  1
2     5  1
3     6  1
4     7  1
5     8  1
6     9  1

$a2
  data2 a2
1     1  2
2     2  2
3     3  2
4     4  2
5     5  2
6     6  2

Comments

0

Maybe not the most elegant solution but this does the job

counter=0
llply(my.list,function(x) {
  counter<<-counter+1
  tmp=data.frame(x,counter)
  colnames(tmp)[3]=paste0("a",counter)
  tmp
})

Comments

0

Another not too elegant solution, starting with

listofdf <- list(a1=data.frame(alfa=c(1,1), beta=c(2,3)), 
                 a2=data.frame(alfa=c(1,2), beta=c(2,5)),
                 a3=data.frame(alfa=c(1,1), beta=c(1,3)) )

then

listofdfplus <- listofdf
for (i in 1:length(listofdf)){ listofdfplus[[i]]$extra <- i 
    names(listofdfplus[[i]]) <- c(names(listofdf[[i]]),names(listofdf)[i]) }

gives

> listofdfplus
$a1
  alfa beta a1
1    1    2  1
2    1    3  1

$a2
  alfa beta a2
1    1    2  2
2    2    5  2

$a3
  alfa beta a3
1    1    1  3
2    1    3  3

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.