Say I have loaded a csv file into R with two columns (column A and column B say) with real value entries. Call the dataframe df. Is there away of speeding up the following code:
dfm <- df[floor(A) = x & floor(B) = y,]
x <- 2
y <- 2
dfm
I am hoping there will be something akin to function e.g.
dfm <- function(x,y) {df[floor(A) = x & floor(B) = y,]}
so that I can type
Any help much appreciated.
The way that’s written right now won’t work for a few reasons:
xandybefore you assigndfm. In other words, the linesx <- 2andy <- 2must come before thedfm <- ...line.Rdoesn’t know whatAandBare, even if you put them inside the brackets of the dataframe that contains them. You need to writedf$Aanddf$B.=is the assignment operator, but you’re looking for the logical operator==. Right now your code is saying “Assign the value x tofloor(A)” (which doesn’t really make sense). You want to tell it “Only choose rows wherefloor(A)equals x”, orfloor(A)==x.So what you want is:
Note that if you want the dataframe to be called
dfm, you don’t want to name the functiondfm, or you will have to erase the function to make the dataframe.