Subsetting a Spatial Data Frame using Input from InputSelect [duplicate] - r

I'm wondering how to use the subset function if I don't know the name of the column I want to test. The scenario is this: I have a Shiny app where the user can pick a variable on which to filter (subset) the data table. I receive the column name from the webapp as input, and I want to subset based on the value of that column, like so:
subset(myData, THECOLUMN == someValue)
Except where both THECOLUMN and someValue are variables. Is there a syntax for passing the desired column name as a string?
Seems to want a bareword that is the column name, not a variable that holds the column name.

Both subset and with are designed for interactive use and warnings against their use within other functions will be found in their help pages. This stems from their strategy of evaluation arguments as expressions within an environment constructed from the names of their data arguments. These column/element names would otherwise not be "objects" in the R-sense.
If THECOLUMN is the name of an object whose value is the name of the column and someValue is the name of an object whose value is the target, then you should use:
dfrm[ dfrm[[THECOLUMN]] == someValue , ]
The fact that "[[" will evaluate its argument is why it is superior to "$" for programing. If we use joran's example:
d <- data.frame(x = letters[1:5],y = runif(5))
THECOLUMN= "x"
someValue= "c"
d[ d[[THECOLUMN]] == someValue , ]
# x y
# 3 c 0.7556127
So in this case all these return the same atomic vector:
d[[ THECOLUMN ]]
d[[ 'x' ]]
d[ , 'x' ]
d[, THECOLUMN ]
d$x # of the three extraction functions: `$`, `[[`, and `[`,
# only `$` is unable to evaluate its argument

This is precisely why subset is a bad tool for anything other than interactive use:
d <- data.frame(x = letters[1:5],y = runif(5))
> d[d[,'x'] == 'c',]
x y
3 c 0.3080524
Fundamentally, extracting things in R is built around [. Use it.

I think you could use the following one-liner:
myData[ , grep(someValue, colnames(myData))]
where
colnames(myData)
outputs a vector containing all column names and
grep(someValue, colnames(myData))
should results in a numeric vector of length 1 (given the column name is unique) pointing to your column. See ?grep for information about pattern matching in R.

Related

Subset my R list by character vector from dataframe

I have an r object, 'd' that is a list. I want a dataframe of references to subsets of this list to make as variables for a function, 'myfunction'. This function will be called thousands of times using rslurm each using a different subset of d.
example: d[['1']][[3]] references a data matrix within the list.
myfunction(d[['1']][[3]])
works fine, but I want to be able to call these subsets from a dataframe.
I want to be able to have a dataframe, 'ds' containing all of my subset references.
>ds
d
1 d[['1']][[3]]
2 d[['1']][[4]]
>myfunction(get(ds[1,1]))
Error in get(ds[1, 1]) : object 'd[['1']][[3]]' not found
Is there something like 'get' that will let me call a subset of my object, d?
Or something I can put in 'myfunction' that will clarify that this string references a subset of d?
stack_overflow 'get'
A list:
my_list <- c('peanut', 'butter', 'is', 'amazing')
A dataframe containing subset references:
my_dataframe <- data.frame(keys=c("my_list[[1]]", "my_list[[2]]", "my_list[[3]]", "my_list[[4]]"), stringsAsFactors=F)
A function that extracts the value from a list based on a passed value:
my_function <- function(key, my_list) {
from_list <- eval(parse(text=key))
print(from_list)
}
Getting the value from a list by passing in the dataframe row choice and the list:
my_function(my_dataframe[1,1], my_list)
I solved this by changing myfunction to take two variables, c and w, and defining d using bracket notation in the first line of the updated function. My ds now has two variables, c and w, with variable c defined as as.character and it works!
myfunction(c,w) {
d<-d[[c]][[w]]
....rest of function}
>ds
c w
1 1 3
2 1 4
>test <- myfunction(ds[1,1],ds[1,2])

R selecting rows from dataframe using logical indexing: accessing columns by `$` vs `[]`

I have a simple R data.frame object df. I am trying to select rows from this dataframe based on logical indexing from a column col in df.
I am coming from the python world where during similar operations i can either choose to select using df[df[col] == 1] or df[df.col == 1] with the same end result.
However, in the R data frame df[df$col == 1] gives an incorrect result compared to df[df[,col] == 1] (confirmed by summary command). I am not able to understand this difference as from links like http://adv-r.had.co.nz/Subsetting.html it seems that either way is ok. Also, str command on df$col and df[, col] shows the same output.
Is there any guidelines about when to use $ vs [] operator ?
Edit:
digging a little deeper and using this question as reference, it seems like the following code works correctly
df[which(df$col == 1), ]
however, not clear how to guard against NA and when to use which
You confused many things.
In
df[,col]
col should be the column number. For example,
col = 2
x = df[,col]
would select the second column and store it to x.
In
df$col
col should be the column name. For example,
df=data.frame(aa=1:5,bb=10:14)
x = df$bb
would select the second column and store it to x. But you cannot write df$2.
Finally,
df[[col]]
is the same as df[,col] if col is a number. If col is a character ("character" in R means the same as string in other languages), then it selects the column with this name. Example:
df=data.frame(aa=1:5,bb=10:14)
foo = "bb"
x = df[[foo]]
y = df[[2]]
z = df[["bb"]]
Now x, y, and z are all contain the copy of the second column of df.
The notation foo[[bar]] is from lists. The notation foo[,bar] is from matrices. Since dataframe has features of both matrix and list, it can use both.
Use $ when you want to select one specific column by name df$col_name.
Use [] when you want to select one or more columns by number:
df[,1] # select column with index 1
df[,1:3]# select columns with indexes 1 to 3
df[,c(1,3:5,7)] # select columns with indexes 1, 3 to 5 and 7.
[[]] is mostly for lists.
EDIT: df[which(df$col == 1), ] works because which function creates a logical vector which checks if the column index is equal to 1 (true) or not (false). This logical vector is passed to df[] and only true value is shown.
Remove rows with NAs (missing values) in data.frame - to find out more about how to deal with missing values. It is always a good practice to exclude missing values from dataset.

using values from a vector in R

Suppose I have a vector x=c(3,2,1). I have a data frame d. I want to add a column in that data frame such that if x takes value 3 then the new column takes value 1 else it takes value 0. It can be done by using simple "ifelse". But my problem is that I want to have the new vector name as "var_3" (without quotes obviously) where this 3 I will extract from x[1].
I have tried:
d$paste("var",x[1],sep="_")=ifelse(d$x==x[1],1,0)
which gives me the error: target of assignment expands to non-language object. As because paste gives me my desired var_3 but with quotes. I have tried noquotes too but with no luck.
This won't work with the $ operator but with the [ subscript operator:
d[, paste("var", x[1], sep="_")] <- ifelse(d$x == x[1], 1, 0)

Subset based on variable column name

I'm wondering how to use the subset function if I don't know the name of the column I want to test. The scenario is this: I have a Shiny app where the user can pick a variable on which to filter (subset) the data table. I receive the column name from the webapp as input, and I want to subset based on the value of that column, like so:
subset(myData, THECOLUMN == someValue)
Except where both THECOLUMN and someValue are variables. Is there a syntax for passing the desired column name as a string?
Seems to want a bareword that is the column name, not a variable that holds the column name.
Both subset and with are designed for interactive use and warnings against their use within other functions will be found in their help pages. This stems from their strategy of evaluation arguments as expressions within an environment constructed from the names of their data arguments. These column/element names would otherwise not be "objects" in the R-sense.
If THECOLUMN is the name of an object whose value is the name of the column and someValue is the name of an object whose value is the target, then you should use:
dfrm[ dfrm[[THECOLUMN]] == someValue , ]
The fact that "[[" will evaluate its argument is why it is superior to "$" for programing. If we use joran's example:
d <- data.frame(x = letters[1:5],y = runif(5))
THECOLUMN= "x"
someValue= "c"
d[ d[[THECOLUMN]] == someValue , ]
# x y
# 3 c 0.7556127
So in this case all these return the same atomic vector:
d[[ THECOLUMN ]]
d[[ 'x' ]]
d[ , 'x' ]
d[, THECOLUMN ]
d$x # of the three extraction functions: `$`, `[[`, and `[`,
# only `$` is unable to evaluate its argument
This is precisely why subset is a bad tool for anything other than interactive use:
d <- data.frame(x = letters[1:5],y = runif(5))
> d[d[,'x'] == 'c',]
x y
3 c 0.3080524
Fundamentally, extracting things in R is built around [. Use it.
I think you could use the following one-liner:
myData[ , grep(someValue, colnames(myData))]
where
colnames(myData)
outputs a vector containing all column names and
grep(someValue, colnames(myData))
should results in a numeric vector of length 1 (given the column name is unique) pointing to your column. See ?grep for information about pattern matching in R.

What is the difference between colnames(x[1])<-"name" and colnames(x)[1]<-"name"?

I want to rename column names in data.frame,
> x=data.frame(name=c("n1","n2"),sex=c("F","M"))
> colnames(x[1])="Name"
> x
name sex
1 n1 F
2 n2 M
> colnames(x)[1]="Name"
> x
Name sex
1 n1 F
2 n2 M
>
Why does colnames(x[1]) = "Name" not work, while colnames(x)[1]="Name" does?
What is the reason? What is the difference betweent them?
The too much information answer:
If you look at what each of the options "de-sugars" to:
# 1.
`[<-`(x, 1, value=`colnames<-`(x[1], 'Name'))
# 2.
`colnames<-`(x, `[<-`(colnames(x), 1, 'Name'))
The first option makes a new data.frame from just the first column, renames that column (successfully), and then tries to assign that data.frame back over the first column. [<-.data.frame will propagate the values, however will not rename existing columns based on the names of value.
The second option gets the colnames of the data.frame, updates the first value, and creates a new data.frame with the updated names.
(Answer to #Peng Peng's question here because I can't figure out how to get backtick quoting to work in a comment...)
The backtick is to quote the variable name. Consider the difference here:
x<-1
`x<-`<-1
The first assigns 1 to a variable called x, but the second assigns to a variable called x<-. These unusal variable names are actually used by the <- primitive function - you are allowed arbitrary function calls on the lhs of an assignment, and a function with <- appended to the name specifies how to perform the update (similar to setf in lisp).
Because you want to modify the column names attribute of x, a data.frame. Hence
colnames(x) <- ....
is correct, whether or not you assign one or more at the same time.

Resources