I have the following dataframe created as:
A <- data.frame(Col1 = c("Item1","Item1","Item1","Item2","Item2","Item2","Item1","Item1","Item2","Item2"), Col2 = 101:110)
Now, I need to create a new dataframe with the condition that it should only include all items of the dataframe A where the value of Col1 = "Item2". That is, I need to get the following:
Traditionally, I can do this simply as follows:
A_new <- A[A$Col1=="Item2",]
(OR)
A_new <- A['$'(A,Col1)=="Item2",]
But rather, I need to do do this via variable names instead, owing to the nature of my actual code. So I define two variables as follows:
var1 <- "Col1"
var2 <- "Item2"
And now when I try to create this new dataframe using the following, I simply get an empty dataframe with the two columns and no rows:
A_new <- A['$'(A,var1)==var2,]
And also, when I apply this exact same logic on my actual code trying to create the subset dataframe, I additionally get an error saying my var1 is invalid subscript type 'language'
Can anyone help me out with creating this subset dataframe by addressing column names and column contents using variables?