pkpkPPkafa pkpkPPkafa - 3 months ago 9
R Question

Arithmetic on multiple columns/rows of dataframe in R

I have dataset with a header (of years) and four columns that designate different characteristica

For each year, I want to divide all observations by 10^6, except for the first four columns. I know one can do

Data[,-1]/(10^6)


if one wants the first column to be left out, but what about if one needs four.

Best,

ID

Answer

We can use the sequence of 1:4, wrap it inside (..) and use the -.

Data[, -(1:4)] <- Data[,-(1:4)]/(10^6)
Comments