divide row by another row if it is a certain comun in r

To divide one row of a DataFrame with all values in a certain column by another row's values in the same column in R, we can use the following code:

main.r
# create sample data
df <- data.frame(A = c(1, 2, 3), B = c(4, 5, 6))

# divide first row by second row for column B
df[1, "B"] / df[2, "B"]
141 chars
6 lines

This code divides value in df's first row and column "B" by the value in the second row and column "B". To divide an entire row, we can use the following code:

main.r
# divide entire first row by second row for column B
df[1, ] / df[2, ]
71 chars
3 lines

This code divides all columns in the first row of df by the corresponding values in the second row.

gistlibby LogSnag