Let the example explain (just a toy example to demo the problem I met in a piece of complicated code). In the following example, why data.frame a
is not changed after sapply
?
> a=data.frame(A=c(1,2,3),B=c(4,5,6))
> a
A B
1 1 4
2 2 5
3 3 6
> a[c(T,T,F),]
A B
1 1 4
2 2 5
> sapply(c(1,2), function(x) a=a[c(T,T,F),])
[,1] [,2]
A Numeric,2 Numeric,2
B Numeric,2 Numeric,2
> a
A B
1 1 4
2 2 5
3 3 6
A second thought, this is likely because of the namespace. The changed a
only exist inside the function. My question is then how to make this global?