Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Referencing other items in a list before it is stored as an object

Tags:

r

I have a list and would like to create a new list entry, d, by binding together the existing list entries as shown below:

library(data.table)

## this works fine
example_list <- list("a" = data.frame(x = 1),
     "b" = data.frame(x = 2),
     "c" = data.frame(x = 3))

example_list[["d"]] <- rbindlist(example_list[c("a", "b", "c")])

Is it possible to create d at the same time as I create the original list? I would like to do something like this:

## this does not work
example_list <- list("a" = data.frame(x = 1),
     "b" = data.frame(x = 2),
     "c" = data.frame(x = 3),
     "d" = rbindlist(.[c("a", "b", "c")]))

Edit: I need to explicitly reference previous list entries, thus something like this would not work:

## ineligible
example_list <- list("a" = data.frame(x = 1),
     "b" = data.frame(x = 2),
     "c" = data.frame(x = 3),
     "d" = data.frame(x = 1) %>% 
       rbind(data.frame(x = 2)) %>% 
       rbind(data.frame(x = 3)))
like image 983
DJC Avatar asked Dec 21 '21 19:12

DJC


People also ask

Does ArrayList store object reference?

ArrayList stores the references of objects.

Can two objects have same reference?

No, it never returns true unless you feed it the same exact object reference. The reason for it is that Java objects are not "embedded" in one another: there is a reference to B inside A , but it refers to a completely different object.

What happens when you copy an object with a reference?

When an object variable is copied, the reference is copied, but the object itself is not duplicated. Now we have two variables, each storing a reference to the same object: As you can see, there’s still one object, but now with two variables that reference it.

What are objects of reference and why are they important?

Learn what objects of reference are, who uses them, how they help to communicate and how to get started. What are objects of reference? An object of reference is a whole physical object, or part of an object, that you hold or touch to represent or identify: A person.

How to use objects and references in JavaScript?

You can use an object as an object variable and a method parameter. You can create a method that returns an object. You can create the method equals, which can be used to check if two objects of the same type have the same contents or state. Let's continue working with objects and references.

How do I cross reference items in a list?

Cross-referencing numbered items in lists (enumerate). Loading the hyperref package, automatically adds links to cross-references and allows navigation to list items by clicking the reference. By default, items in a description can’t be cross-referenced.


Video Answer


2 Answers

If we want to use a %>%, wrap it inside {}

library(dplyr)
library(data.table)
list("a" = data.frame(x = 1),
     "b" = data.frame(x = 2),
     "c" = data.frame(x = 3)) %>%
     {c(., d = list(rbindlist(.[c("a", "b", "c")])))}

In base R, we can get the data from a list using within.list

within.list(list("a" = data.frame(x = 1),
     "b" = data.frame(x = 2),
     "c" = data.frame(x = 3)), d <- rbindlist(list(a, b, c)))

-output

$a
  x
1 1

$b
  x
1 2

$c
  x
1 3

$d
   x
1: 1
2: 2
3: 3
like image 76
akrun Avatar answered Oct 23 '22 08:10

akrun


I don't think base R supports that (nor a package I can think of without similar hacks). I'm inferring that you want to do this without leaving individual frames (e.g., a, b) in the main environment, so we can use a local environment to do what we want.

example_list <- local({
  a <- data.frame(x = 1)
  b <- data.frame(x = 2)
  c <- data.frame(x = 3)
  d <- rbindlist(list(a, b, c))
  list(a=a, b=b, c=c, d=d)
})
like image 38
r2evans Avatar answered Oct 23 '22 06:10

r2evans