Reputation: 1143
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.
Upvotes: 3
Views: 97
Reputation: 17412
The way that's written right now won't work for a few reasons:
x
and y
before you assign dfm
. In other words, the lines x <- 2
and y <- 2
must come before the dfm <- ...
line.R
doesn't know what A
and B
are, even if you put them inside the brackets of the dataframe that contains them. You need to write df$A
and df$B
.=
is the assignment operator, but you're looking for the logical operator ==
. Right now your code is saying "Assign the value x to floor(A)
" (which doesn't really make sense). You want to tell it "Only choose rows where floor(A)
equals x", or floor(A)==x
.So what you want is:
dfm.create <- function(x,y) {df[floor(df$A)==x & floor(df$B)==y,]}
dfm <- dfm.create(2,2)
Note that if you want the dataframe to be called dfm
, you don't want to name the function dfm
, or you will have to erase the function to make the dataframe.
Upvotes: 3