Jusep
Jusep

Reputation: 187

R: Dividing different rows and creating a new colum with its value

I would like to create a function that duplicates the columns of a dataframe (df1). This type of dataframe contains different rows with the GDP of different countries, and one last row with the Median of each column:

        GDP    per_capita    
France   2      3 
Spain    2      3 
Ethiopia 4      9 
Medians  2      3 

My objective is duplicating the columns and, in the new two columns (GDP_1 and per_capita_1), divide each GDP by its median. So it would be like this:

        GDP    per_capita    GDP_1   per_capita_1
France   2      3              2/2      3/3
Spain    2      3              2/2      3/3
Ethiopia 4      9              4/2      9/3
Medians  2      3              2/2      3/3

So at the end we would have this:

        GDP    per_capita    GDP_1   per_capita_1
France   2        3            1      1
Spain    2        3            1      1
Ethiopia 4        9            2      3
Medians  2        3            1      1
   

I'm not sure if I should duplicate the columns and change the value of the new ones, or maybe adding two columns more. But I find quite difficult setting the division.

Any clue? Thanks!

Upvotes: 1

Views: 45

Answers (3)

Pal R.K.
Pal R.K.

Reputation: 118

I have assumed the Medians are in the last row:

library(dplyr) 

df %>%
   mutate(GDP_1 = GDP/last(GDP),
          per_capita_1 = per_capita/last(per_capita))

Upvotes: 0

ThomasIsCoding
ThomasIsCoding

Reputation: 101179

Here is another base R option using t

cbind(df, `colnames<-`(t(t(df) / unlist(df[nrow(df), ])), paste0(names(df), "_1")))

which gives

         GDP per_capita GDP_1 per_capita_1
France     2          3     1            1
Spain      2          3     1            1
Ethiopia   4          9     2            3
Medians    2          3     1            1

Upvotes: 0

akrun
akrun

Reputation: 886998

We extract the last row of the dataset and divide with the dataset after making the lengths same, and assign the output to new columns

df[paste0(names(df), "_1")] <-  df/unlist(df[nrow(df),])[col(df)]

-output

df
#         GDP per_capita GDP_1 per_capita_1
#France     2          3     1            1
#Spain      2          3     1            1
#Ethiopia   4          9     2            3
#Medians    2          3     1            1

Or use sweep

df[paste0(names(df), "_1")] <- sweep(df, 2, unlist(df[nrow(df), ]), `/`)

It can be also wrapped into a function

f1 <- function(dat) {
     dat[paste0(names(dat), "_1")] <-  dat/unlist(dat[nrow(dat),])[col(dat)]
    return(dat)
  }
f1(df)
#          GDP per_capita GDP_1 per_capita_1
#France     2          3     1            1
#Spain      2          3     1            1
#Ethiopia   4          9     2            3
#Medians    2          3     1            1

data

df <- structure(list(GDP = c(2L, 2L, 4L, 2L), per_capita = c(3L, 3L, 
9L, 3L)), class = "data.frame", row.names = c("France", "Spain", 
"Ethiopia", "Medians"))

Upvotes: 2

Related Questions