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
Round a Posix Date (Posixct) with Base R Functionality
Extracting Unique Rows from a Data Table in R
Reshape Multiple Categorical Variables to Binary Response Variables
How to Find the Indices of the Top 10,000 Elements in a Symmetric Matrix(12K X 12K) in R
How to Annotate a Reference Line at the Same Angle as the Reference Line Itself
How to Cross-Paste All Combinations of Two Vectors (Each-To-Each)
Get Connected Components Using Igraph in R
R Solve:System Is Exactly Singular
How to Deal with Spaces in Column Names
Ggplot2: Drop Unused Factors in a Faceted Bar Plot But Not Have Differing Bar Widths Between Facets
Scale and Size of Plot in Rstudio Shiny
Simple Examples of Filter Function, Recursive Option Specifically
R Random Forest Error - Type of Predictors in New Data Do Not Match
R Tm Package Vcorpus: Error in Converting Corpus to Data Frame