Undefined Columns Selected When Subsetting Data Frame

Undefined columns selected when subsetting data frame

You want rows where that condition is true so you need a comma:

data[data$Ozone > 14, ]

R - Undefined columns selected when subsetting data frame inside a function

I think you need get around your outcome1 in your function definition, as you are passing a string rather than an object as your argument. With this example data:

outcome <- data.frame(Pneumonia = sample(0:1, size = 5, replace = TRUE),
State = c("NY", "NY", "NY", "CA", "CA"),
Hospital.Name = LETTERS[1:5]
)

And this modified function:

best <- function(df_, state_, var_) {
subset(df_, State == state_, select = c(Hospital.Name, get(var_)))
}

Now you can call it more or less as before:

> best(df_ = outcome, state_ = "NY", var_ = "Pneumonia")
Hospital.Name Pneumonia
1 A 0
2 B 1
3 C 0

Error in data frame undefined columns selected when reordering colum in r

You should try as.vector:

wil_test_SNV1 <- wil_test_SNV[,as.vector(cn3)]

How to fix 'Undefined columns selected' error in R?

You can use apply with an all(is.na) function to get the index of the all NA rows. Then remove them from the dataframe. I've created an example here:

movieID <- c(1:5)
movieTitle <- c("Movie1", "Movie2", "Movie3", "Movie4", "Movie5")
student1 <- c(1, NA, 2:4)
student2 <- c(2, NA, 2, NA, 4)
student3 <- c(NA, NA, 1:3)

ratingMovie <- data.frame(movieID, movieTitle, student1, student2, student3)

movieID movieTitle student1 student2 student3
1 1 Movie1 1 2 NA
2 2 Movie2 NA NA NA
3 3 Movie3 2 2 1
4 4 Movie4 3 NA 2
5 5 Movie5 4 4 3
>

index <- apply(ratingMovie[, c(3:5)], 1, function(x) all(is.na(x)))
ratingMovie <- ratingMovie[!index,]

movieID movieTitle student1 student2 student3
1 1 Movie1 1 2 NA
3 3 Movie3 2 2 1
4 4 Movie4 3 NA 2
5 5 Movie5 4 4 3
>

Determine which column name is causing 'undefined columns selected' error when using subset()

Find the elements of your vector that are not %in% the names() of your data frame.

Working example:

dd <- data.frame(a=1,b=2)
subset(dd,select=c("a"))
## a
## 1 1

Now try something that doesn't work:

v <- c("a","d")
subset(dd,select=v)
## Error in `[.data.frame`(x, r, vars, drop = drop) :
## undefined columns selected

v[!v %in% names(dd)]
## [1] "d"

Or

setdiff(v,names(dd))
## [1] "d"

The last few lines of the example code in ?match show a similar case.



Related Topics



Leave a reply



Submit