Linked Questions
234 questions linked to/from Dynamically select data frame columns using $ and a character value
21
votes
1
answer
51k
views
How to use a string variable to select a data frame column using $ notation [duplicate]
From the reading I've been doing with R, I can select a column in a data frame by either of these two methods: frame[,column] or frame$column. However, when I have a string as a variable, it works ...
8
votes
2
answers
24k
views
Select list element programmatically using name stored as string [duplicate]
I have a list
myList = list(a = 1, b = 2)
names(myList)
# [1] "a" "b"
I want to select element from 'myList' by name stored in as string.
for (name in names(myList)){
print (...
6
votes
5
answers
2k
views
Using a string from a list to select a column in R [duplicate]
I have a list of variable names and I want to use the strings from the list to access columns in data frames
list<-list("Var1", "Var2", "Var3")
df1 <- data.frame(&...
2
votes
2
answers
12k
views
How to pass string as column name in R [duplicate]
m ="jan"
n = "5e"
r = paste0("jan","5e","_results")
"jan5e_results"
Now I want to use r's value and select a data frame because I have data frame with names like
jan5e_results
feb5e_results
...
5
votes
3
answers
747
views
define $ right parameter with a variable in R [duplicate]
I would like to pass a variable to the binary operator $.
Let's say I have this
> levels(diamonds$cut)
[1] "Fair" "Good" "Very Good" "Premium" "Ideal"
Then I want to make a function ...
-2
votes
2
answers
5k
views
R - Getting Column of Dataframe from String [duplicate]
I am trying to create a function that allows the conversion of selected columns of a data frame to categorical data type (factor) before running a regression analysis.
Question is how do I slice a ...
0
votes
1
answer
5k
views
R : creating new data from column with string variable name [duplicate]
I have string variable, str, with the value "car". I would like to create a new column in my data frame using str, but creates a column with the name "car". Below is a short example.
a<-c("Bill"...
1
vote
1
answer
3k
views
R loop over string and use it to refer to column names [duplicate]
I have data frame with column names 1990.x ..2000.x, 1990.y,..2000.y. I want to replace NAs in variables ending with ".x" with values from .y from corresponding year. It is element by element ...
2
votes
1
answer
2k
views
Functionally add new column based on division of two others [duplicate]
Background
Sorry if this is a repeat, I couldn't find an exact match to this question.
So as part of a larger function, I'm trying to add a new column in a data.frame which is basically the division ...
0
votes
2
answers
1k
views
How to refer to columns in a table via a character variable in r? [duplicate]
Probably a fairly simple question, but couldn't find anything specific for it.
Lets say I have the following data frame
A
Hello World
1 5 6
2 5 6
If you want to fetch the the ...
0
votes
2
answers
2k
views
Filter data frame inside a function with a column argument [duplicate]
I'm trying to update a data frame column inside a function based on a filtered column.
#example dataframe
my.df = data.frame(A=1:10)
#define function to classify column passed as argument 2 based on ...
0
votes
1
answer
2k
views
How to access a column in a dataframe when column name is stored in a string in R [duplicate]
I want to access a column in a dataframe, and the column name is stored in a string variable.
I have found this question answered for other languages, and for accesing a variable, but not for a ...
0
votes
1
answer
2k
views
Error in `$<-.data.frame`(`*tmp*`, group, value = integer(0)) [duplicate]
This code works fine:
d0 = DGEList(raw)
group = as.factor(metadata$Benefit)
d0$samples$group = group
keep.exprs = edgeR::filterByExpr(d0, group = group)
d0 = d0[keep.exprs,, keep.lib.sizes = FALSE]
d0 ...
0
votes
1
answer
2k
views
R assign value dynamically to element of data frame [duplicate]
I need to write a line of R that can reference an element of a list using a dynamically generated name.
For example, if I have the list:
x <- list(a = "Foo", b = "Bar", c = "Salad")
And a ...
0
votes
1
answer
972
views
sum in function not working properly [duplicate]
I want to create a function to count the values in a certain variable in a subsetted dataset, but my function is not working as it supposed to.
selected_cyl_6 <- subset(mtcars, mtcars$cyl==6)
...