Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Losing data by using the function unlist

Tags:

r

I have a simple but strange problem.

indices.list is a list, containing 118,771 Elements(integers or numeric). By applying the function unlist I lose about 500 elements.

Look at the following code:

> indices <- unlist(indices.list, use.names = FALSE)
> 
> length(indices.list)
[1] 118771
> length(indices)
[1] 118248

How is that Possible?? I checked if indices.list contains any NA. But it does not:

> any(is.na(indices.list) == TRUE)
[1] FALSE

data.set.merged is a dataframe containing more than 200,000 rows. When I use the vector indices (which apparently has the length 118,248) in order to get a subset of data.set.merged, I get a dataframe with 118,771 rows!?? That's so strange!

data.set.merged.2 <- data.set.merged[indices, ]
> nrow(data.set.2)
[1] 118771

Any ideas whats going on here?

like image 774
Hagen Brenner Avatar asked Jan 01 '12 23:01

Hagen Brenner


1 Answers

Well, for your first mystery, the likely explanation is that some elements of indices.list are NULL, which means they will disappear when you use unlist:

unlist(list(a = 1,b = "test",c = 2,d = NULL, e = 5))
     a      b      c      e 
   "1" "test"    "2"    "5" 
like image 91
joran Avatar answered Sep 28 '22 22:09

joran