Invalid dplyr index list

I encountered an error in the script that I am writing, this only happens when dplyr starts. I first came across this when I found a function from dplyr that I wanted to use, after which I installed and launched the package. Here is an example of my error:

First I read in a table from excel that has the column values ​​that I will use as indices in it:

 library(readxl) examplelist <- read_excel("example.xlsx") 

File contents:

 1 2 3 4 1 1 4 1 2 3 2 1 4 4 1 4 

And then I create a data frame:

 testdf = data.frame(1:12, 13:24, 25:36, 37:48) 

And then I have a loop that calls a function that uses examplelist values ​​as indexes.

 testfun <- function(df, a, b, c, d){ value1 <- df[[a]] value2 <- df[[b]] value3 <- df[[c]] value4 <- df[[d]] } for (i in 1:nrow(examplelist)){ testfun(testdf, examplelist[i, 1], examplelist[i, 2], examplelist[i, 3], examplelist[i, 4]) } 

When I run this script without dplyr , everything is fine, but with dplyr it gives me an error:

  Error in .subset2(x, i, exact = exact) : invalid subscript type 'list' 

Why did dplyr error dplyr and how to fix it?

+5
source share
2 answers

I think the MKR answer is the right decision, I will tell a little more about why with some alternatives.

The readxl library is part of tidyverse and returns tibble ( tbl_df ) using the read_excel function. This is a special type of data frame, and there are differences from the behavior of the database, especially for printing and a subset (read here ).

Tiblis also clearly indicates [ and [[ : [ always returns another Tibet, [[ always returns a vector. More drop = FALSE

So now you can see that your examplelist[i, n] returns a character, not a vector of length 1, so using as.numeric works.

 library(readxl) examplelist <- read_excel("example.xlsx") class(examplelist[1, 1]) # [1] "tbl_df" "tbl" "data.frame" class(examplelist[[1, 1]]) # [1] "numeric" class(as.numeric(examplelist[1, 1])) # [1] "numeric" class(as.data.frame(examplelist)[1, 1]) # [1] "numeric" 

My workflow tends to use tidyverse , so you can use [[ for a subset or as.data.frame if you don't need tibbles.

+4
source

I see this problem even without loading dplyr . It seems that the culprit is the use of examplelist elements. if you print the value of examplelist[1, 2] , then this is a 1x1 frame size. But it is expected that the value of a, b, c and d will be a prime number. Therefore, if you change examplelist[i, 1] , etc., using as.numeric , then this will be avoided. Modify the testfun call as follows:

 testfun(testdf, as.numeric(examplelist[i, 1]), as.numeric(examplelist[i, 2]), as.numeric(examplelist[i, 3]), as.numeric(examplelist[i, 4])) 
+2
source

Source: https://habr.com/ru/post/1273946/


All Articles