3

If I have a list like:

list <- list( "1" = data.frame(time=1:3, temp = sample(11:13)),
              "3" = data.frame(time=1:3, temp = sample(11:13)))

list

$`1`
  time temp
1    1   11
2    2   12
3    3   13

$`3`
  time temp
1    1   11
2    2   12
3    3   13

Now I want to add a correction-value to columns temp, +1 for dataframe 1 and -1 for dataframe 3, so the result would be:

$`1`
  time temp
1    1   12
2    2   13
3    3   14

$`3`
  time temp
1    1   10
2    2   11
3    3   12

Let´s additionally assume I have multiple of those lists, where sometimes dataframe 3 or 1 may be missing, or even a dataframe 2 maybe included, which would need its own correction-factor... I tried strange things for dataframe 1:

list <- lapply(list, function(x)   {x <- x$"1"$temp-1;x})

or

list <- lapply(list, function(x)   {x <- x[x$temp+1,];x})

also tried to add a seq_along for the other dataframe in the list...nothing works, maybe because I don´t quite understand how the syntax works...

3
  • 2
    I'd give them a solid chance to edit what clearly is a snafu before downvoting...patience! Commented Aug 19, 2011 at 16:15
  • Ok I waited for the full question. But this is still too ambiguous to be answerable. Build a reproducible example that actually represents your full problem. Commented Aug 19, 2011 at 17:17
  • If this data is all the same size, it may be better to store it as a data.frame(), it would certainly simplify operations. If you want a quick and dirty way to convert back to a data.frame you can use df <- ldply(mylist) from the plyr package. Commented Aug 19, 2011 at 18:21

1 Answer 1

2

I changed the name of the data structure to dflist. It's just wrong to call a list "list". You also need some data structure to pull in the associated correction factors, so let's posit one with the same names as "dflist":

dflist <- list( "1" = data.frame(time=1:3, temp = sample(11:13)),
              "3" = data.frame(time=1:3, temp = sample(11:13)))

corrlist <- list("1" = 1, "3"=-1) 

# Replaced lapply with for loop
for( nam in names(dflist)) {
        dflist[[nam]]['temp'] <- dflist[[nam]]['temp'] +corrlist[[nam]]
                            }
 dflist
Sign up to request clarification or add additional context in comments.

6 Comments

Perfect, thanks! Except a missing bracket at the end - and it deletes/overwrites the names/numbers of the dataframes. Instead of $1 and $3 the dataframes are marked with [[1]] and [[2]]. If I return(dflist[x]) the names stay, but also those [[1]] are doubled O_o
The bracket was there (added space to emphasize) but there was a missing paren and your point about what I returned is well taken. Should have just returned dflist.
But other problems remain. Needs improvement.
Thanks for the loop. Looks as it should work....Unfortunately I also can´t figure out where the error is...
I don't get an error with that code. Is your R installation corrupted?
|

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.