Home > Software design >  Divide by a certain position in R
Divide by a certain position in R

Time:08-19

I have several series, each one indicates the deflator for the GDP for each country. (Data attached down below)

So what I want to do is to divide every column for the 97th position.

I know this could be pretty simple for you, but I am struggling.

This is my code so far:

d_data <- d_data  %>% 
  mutate_if(is.numeric, function(x) x/d_data[[97,x]])

So as you can see in the data, from columns 3 to 8 data are numeric.

I think the error is that argument x of the function refers to the column name, while in the d_data, the second argument refers to column position and that is the main issue.

How can I solve this? Thanks in advance!!

Data


Data was massive to put here (745 rows, 8 columns)

So I uploaded the dput(d_data) output here

CodePudding user response:

Use mutate with across as _at/_all are deprecated. Also, to extract by position, use nth

library(dplyr)
d_data  %>% 
     mutate(across(where(is.numeric), ~ .x/nth(.x, 97)))

In the OP's code, instead of d_data[[97,x]], it should be x[97] as x here is the column value itself

d_data  %>% 
  mutate_if(is.numeric, function(x) x/x[97])

If we want to subset the original data column, have to pass either column index or column name. Here, x doesn't refer to column index or name. But with across, we can get the column name with cur_column() e.g. (mtcars %>% summarise(across(everything(), ~ cur_column()))) which is not needed for this case

  • Related