xm1
xm1

Reputation: 1765

R .Last.value error

I have to loop through a list and when I index it, .Last.value stops working. Example:
A list:

x
[[1]]
    cd_entrada   qx laplace     r
21           1 0.65      50 16.35
141          1 0.65       0 11.24
81           1 0.80       0  2.87
181          0 0.75      25  2.68

[[2]]
   cd_entrada   qx penalty stepno     r
1           0 0.40      10   1500 12.64
2           1 0.45     400   2000 21.58
3           1 0.35     400    750 19.38
4           1 0.45     400   1000 12.78
5           1 0.55     400   1500  4.77
6           1 0.50     400    750  4.79
7           1 0.65     400    750  1.80
8           1 0.75     100    750  1.80
9           2 0.65     100   1500  1.95
10          0 0.40      10    750  9.18
11          0 0.35     200   1000  7.90
12          1 0.45     100   1500  5.65
13          0 0.60     100    750  2.80

Filtering lines of first element

(x[[1]])[.Last.value$cd_entrada==0 & .Last.value$laplace==25,]
    cd_entrada   qx laplace    r
181          0 0.75      25 2.68

It is ok. But When I replace 1 by i, it gives error:

i=1
(x[[i]])[.Last.value$cd_entrada==0 & .Last.value$laplace==25,]
Error in .Last.value$cd_entrada : 
  $ operator is invalid for atomic vectors

But .Last.value stills returning the same data.frame:

(x[[i]]); .Last.value
    cd_entrada   qx laplace     r
21           1 0.65      50 16.35
141          1 0.65       0 11.24
81           1 0.80       0  2.87
181          0 0.75      25  2.68
    cd_entrada   qx laplace     r
21           1 0.65      50 16.35
141          1 0.65       0 11.24
81           1 0.80       0  2.87
181          0 0.75      25  2.68

P.S.

> dput(x)
list(structure(list(cd_entrada = c(1, 1, 1, 0), qx = c(0.65, 
0.65, 0.8, 0.75), laplace = c(50, 0, 0, 25), r = c(16.35, 11.24, 
2.87, 2.68)), .Names = c("cd_entrada", "qx", "laplace", "r"), class = "data.frame", row.names = c(21L, 
141L, 81L, 181L)), structure(list(cd_entrada = c(0, 1, 1, 1, 
1, 1, 1, 1, 2, 0, 0, 1, 0), qx = c(0.4, 0.45, 0.35, 0.45, 0.55, 
0.5, 0.65, 0.75, 0.65, 0.4, 0.35, 0.45, 0.6), penalty = c(10, 
400, 400, 400, 400, 400, 400, 100, 100, 10, 200, 100, 100), stepno = c(1500, 
2000, 750, 1000, 1500, 750, 750, 750, 1500, 750, 1000, 1500, 
750), r = c(12.64, 21.58, 19.38, 12.78, 4.77, 4.79, 1.8, 1.8, 
1.95, 9.18, 7.9, 5.65, 2.8)), .Names = c("cd_entrada", "qx", 
"penalty", "stepno", "r"), class = "data.frame", row.names = c(NA, 
-13L)))

Upvotes: 0

Views: 91

Answers (1)

IRTFM
IRTFM

Reputation: 263411

.Last.value will change every time you do another assignment such as x=1. At that point, .Last.value will just be a length-1 vector with the value 1. You cannot use .Last.value in a function or in a subsetting operation. Only reliable when you want to retrieve something that you might not have assigned to a name. As an example, you've just entered a complex ggplot2 call and don't want to scrape the result from you console and then edit out the extra line-continuation "+"-signs. You could just execute:

 my_last_plot <- .Last.value

I think you might tail() or you might want to be subsetting x[[1]] with a logical expression, but your first result was almost surely a lucky accident based on your having just examined x[[1]] (but then not showing us that code sequence.) See what happens when I first assign your dput output to x

x <- 
list(structure(list(cd_entrada = c(1, 1, 1, 0), qx = c(0.65, 
0.65, 0.8, 0.75), laplace = c(50, 0, 0, 25), r = c(16.35, 11.24, 
2.87, 2.68)), .Names = c("cd_entrada", "qx", "laplace", "r"), class = "data.frame", row.names = c(21L, 
141L, 81L, 181L)), structure(list(cd_entrada = c(0, 1, 1, 1, 
1, 1, 1, 1, 2, 0, 0, 1, 0), qx = c(0.4, 0.45, 0.35, 0.45, 0.55, 
0.5, 0.65, 0.75, 0.65, 0.4, 0.35, 0.45, 0.6), penalty = c(10, 
400, 400, 400, 400, 400, 400, 100, 100, 10, 200, 100, 100), stepno = c(1500, 
2000, 750, 1000, 1500, 750, 750, 750, 1500, 750, 1000, 1500, 
750), r = c(12.64, 21.58, 19.38, 12.78, 4.77, 4.79, 1.8, 1.8, 
1.95, 9.18, 7.9, 5.65, 2.8)), .Names = c("cd_entrada", "qx", 
"penalty", "stepno", "r"), class = "data.frame", row.names = c(NA, 
-13L)))

(x[[1]])[.Last.value$cd_entrada==0 & .Last.value$laplace==25,]
#[1] cd_entrada qx         laplace    r         
#<0 rows> (or 0-length row.names)

Not the result you got, but if we first look at x[[1]]:

 x[[1]]
#    cd_entrada   qx laplace     r
#21           1 0.65      50 16.35
#141          1 0.65       0 11.24
#81           1 0.80       0  2.87
#181          0 0.75      25  2.68

 (x[[1]])[.Last.value$cd_entrada==0 & .Last.value$laplace==25,]
#    cd_entrada   qx laplace    r
#181          0 0.75      25 2.68

If you wanted to do that operation reliably use this code instead:

i=1
x[[i]][ x[[i]]$cd_entrada==0 & x[[i]]$laplace==25, ]
#-----------
    cd_entrada   qx laplace    r
181          0 0.75      25 2.68

Upvotes: 2

Related Questions