Intellipaat Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in R Programming by (50.2k points)

Given a data frame with 3 columns, I want to add a bunch of columns x_k with k running from 1 to N

df

>    a    b    c 

1:   1    1    1

2:   1    2    1

3:   1    3    2

For each new column x_k, its value is calculated by the equation cos(2 * pi * c / k) grouped by a and b.

1 Answer

0 votes
by (107k points)

You can try the following in R programming:

df[, paste0("x_", 1L:N) := lapply(1L:N, function(k) cos(2 * pi * c / k)), .(a, b)]

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...