How to calculate yearly differences of two columns


I am a total newbie in R and I am currently trying to figure out how to calculate the yearly differences between two columns.

I have a dataset (?) containing the years from 2010-2016 with the yearly export / import. I am supposed to create a new variable or vector for the yearly differences in imports and

How would I be able to do that?

I've started and made a vector:

vec 1 = c(trade_data$import, trade_data$export)

however, that doesn't work at all. I have tried to search for similar questions, but I couldn't adapt to it. So I hope someone here can help me out :slight_smile:

Thanks in advance!


trade_data$import_minus_export <- trade_data$import - trade_data$export

It worked!

Thanks a lot :slight_smile:

I also have a table with the columns: 1) year 2) import 3) export

I am supposed to use indexing to find the value of imports in 2010 and 2014. I am then supposed to assign these values to a new object using the concatenate function. How would I do this?

I've started with the following:


However, this doesn't work. But if I for example us 1 instead, it will take the first year in the column.
Is it possible to do so, that I can just write in the year I want to index?

Thanks in advance!

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.