Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error in vapply ... values must be length 1, but FUN(X[[11]]) result is length 0

Tags:

r

I've never been very skilled with R and am coming back after an absence so I'm re-learning a lot. I've got a dataset (named data) that has fields latitude and longitude. Some of the observations have '0' in these fields, which is invalid data. I'm writing an R notebook to document my findings.

I have:

    Let's start by finding out how many records have 0 for latitude and longitude. If it's a great deal of records, we might not be able to rely on these fields:
```{r}
nrow(filter(data, latitude == 0.0))
nrow(filter(data, longitude == 0.0))
```
Okay, there are 12 rows that have 0 for latitude and 12 rows that have 0 for longitude. I'm willing to bet these are the same rows. Let's find out.

```{r}
filter(data, latitude == 0.0)
```

The first two lines that start with nrow() both display the output I expect:

[1] 12
[1] 12

However, the same filter statement, which I expect to output the 12 rows that match the filter criteria, gives me an error when I run the chunk:

Error in vapply(x, obj_sum, character(1L)) : values must be length 1,
 but FUN(X[[11]]) result is length 0

I don't understand why I'm getting this error. The 'data' variable is a tibble, if that makes a difference.

I'd sure appreciate an explanation of what's happening here.

like image 832
Nevo Avatar asked Feb 23 '17 21:02

Nevo


3 Answers

I don't have the reputation to add a comment, so will provide my 2 cents as an answer though it doesn't actually answer the question.

I got a similar error, and I suspect it is related to list columns in your data. Here is a reproducible example:

```{r}
library (dplyr)
library (purrr)

# create data
mydata <- 
  tibble(col_a = rep(c("a", "b"), 5)) %>% 
  mutate(col_b = map(col_a, function (x) { list(a = x, b = x, c = x) }))

# filter
mydata %>% filter (col_a == "a")
```

The last line of code performs as expected when I copy/paste it into the console. However, when I runt it from the R-markdown document (using the 'Ctrl+Enter'-shortcut in R-studio) it produces the error:

Error in vapply(x, obj_sum, character(1L)) : values must be length 1,
 but FUN(X[[1]]) result is length 3

The final "length"-part of the error message depends on the length of the list in col_b.

Don't know what to make of this. A work around may be to reformat any list columns?

like image 91
crlwbm Avatar answered Sep 22 '22 22:09

crlwbm


Your rmarkdown script may contain in-line code that cannot be coerced into a string, before or after your code chunks.

For example, `make_a_graph()` embedded in the text body of the rmarkdown like this:

Next, we want to make_a_graph() with our data.

I solved a similar problem in my script by converting in-line code to chunks one-by-one and the problem went away. Found the tip here.

like image 29
Mark K Avatar answered Sep 23 '22 22:09

Mark K


I do not have sufficient reputation to comment, but I also found that an inline r list(*data_list*) snippet in my R notebook was the culprit for this error. Inserting this segment into a chunk was the solution.

like image 43
romsdocs Avatar answered Sep 23 '22 22:09

romsdocs