Log of other variable

I need to create a new variable in my dataset. I need to find the logarithm of other variable.
My dataset is gdp per year (2016-2021) for columns and countries for rows.

I have find this equation (dataset4$log_GDP<- log(dataset4$gdp_2016, dataset4$gdp_2017, dataset4$gdp_2018, dataset4$gdp_2019, dataset4$gdp_2020, dataset4$gdp_2021)) which allows me to create a new variable (log_gdp) in my dataset but the fact is that i have the same response for each country (12.25947) so that my last column (log_gdp) is only filled with 12.25947.

Do you know how to resolve it and having a different log_gdp for each country ?

Best regards

We need a FAQ: How to do a minimal reproducible example ( reprex ) for beginners

A handy way to supply some sample data is the dput() function. In the case of a large dataset something like dput(head(mydata, 100)) should supply the data we need.

And there is no need to use your actual data. Just indicate schematically what your data look like.
A simple example with three 'countries' and two years follows.
If you data is different then show us how.
A possible solution using the very important package dplyr is also given.

df1 <- data.frame(
  country= c("FR","GE","US"),
  gpd_2016 = c(10,12,18),
  gpd_2017 = c(11,13,20) 

#> Attaching package: 'dplyr'
#> The following objects are masked from 'package:stats':
#>     filter, lag
#> The following objects are masked from 'package:base':
#>     intersect, setdiff, setequal, union

df1_log <- df1 |>
  ) |>
#>   country lgpd_2016 lgpd_2017
#> 1      FR  1.000000  1.041393
#> 2      GE  1.079181  1.113943
#> 3      US  1.255273  1.301030
Created on 2022-11-26 with reprex v2.0.2
1 Like

it doesn't work, don't know why
But as you might not know, is that i need to keep the other data (gdp_2016…) and just create another one that includes the log of gdp.
Thanks !

We cant see your screen. You have to share. This means error messages, poor output. I.e. relevant facts.
Also if you would wish your own dataframe to be considered as a starting point rather than the example the HanOostdijk kindly constructed for you then its in your own power and only yours to do so... See the reprex guide that has already been shared with you.

Good luck solving your problem.


@nirgrahamuk gives excellent advice. You really have to remember that you have all sorts of things in front of you that none of the rest of us can see.

As a likely hint, log() generally only takes one argument. When you give it a second argument it changes the base for the logarithm, which may account for the strange answer you are getting.

1 Like

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.