Ezequiel Garay
Ezequiel Garay

Reputation: 37

R - work within a data frame

I want to operate with some variables (v1, v2 and v3) of a data frame (A). Here's what I want to do:

sum(A$v1*A$v2)/sum(A$v3)

It's annoying to write every time A$varname, how can I tell R to work with that dataframe?

I've seen I can write:

attach(A)
sum(v1*v2)/sum(v3)
detach(A)

but I guess there should be something easier. Thanks in advance

Upvotes: 0

Views: 102

Answers (1)

talat
talat

Reputation: 70336

You can use with() for that purpose:

with(A, sum(v1*v2)/sum(v3))

This will also be "safer" than using attach() and detach() and hence is considered better practice than attach()

Upvotes: 4

Related Questions