Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

No way to append a list in place? Wtf #22

Open
ifly6 opened this issue Apr 25, 2018 · 1 comment
Open

No way to append a list in place? Wtf #22

ifly6 opened this issue Apr 25, 2018 · 1 comment

Comments

@ifly6
Copy link

ifly6 commented Apr 25, 2018

In Java, I have a list (lets say implementation is ArrayList). It's a happy list. And you can add things do that list, because lists are not arrays and you might some time want to add something to it.

List<Integer> ints = new ArrayList<>();
ints.add(1); // works

In Python, I too can have a list. And it's a happy list. You too can add things do this list, because you might want to do that some time. So in our world, where we have a number of data files, the number of which might not be pre-established (something of a reasonable and common use-case), we can do this:

dfs = []
for file in files:
    df = df_supplier(file)  # eg -- pandas.read_csv(file)
    dfs.append(df)

You want to that in R? You're out of luck. The easiest way I can find to do this is to get yourself the package rlist and then do this:

library(rlist)
dfs = list()
for i : 1:length(files) {
    file = files[[i]]
    dfs = list.append(dfs, read_XYZ(file, arguments...))
}

This way, you can have the privilege of wasting your computer time and hard-earnt electricity copying data you already have to a location in which it already is. This is pRogRess! But if you don't want to install a whole library just do this, do not be afraid!

Just write yourself a function,

append = function(li, obj) {
    name <- deparse(substitute(li))
    li[[length(li) + 1]] = obj
    assign(name, li, envir=parent.frame())
    return()
}

Now, with the power of R, you can pass your list, have the whole thing deparsed, add a single thing to the end of the list, and then manually assign that whole thing back to the parent environment's entry of the list. Fortunately, you still have the glorious featuRe of wasting your computer time and hard-earnt electricity copying data you already have to a location in which it already is! Efficiency! (The word efficiency has no Rs in it, that's how you know it's efficient.)

@dwinsemius
Copy link

There is already an append function in base R. Look it up. It can do what you desire as well as insert values into the middle of lists. (Note that lists are vectors, so do not make the incorrect conclusion that this only applies to atomic vectors.)

The requirement that it be done "in place" is simply not an R feature. R is a functional language. If you don't like it, then use a different tool.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants