0

So I have a matrix with 2 columns, first the name, second some content like

name   content
ID1    1,2,3,4
ID2    1,2,3,4,5,6
ID3    1,2,3

The content has different lengths, and splitting the content itself is no problem. After the split I apply a certain function func(x). In the end, I want to receive this output:

name    content
ID1     func(1) 
ID1     func(2) 
ID1     func(3) 
ID1     func(4) 
ID2     func(1) 
ID2     func(2) 
ID2     func(3) 
ID2     func(4)
ID2     func(5)
ID2     func(6)
ID3     func(1) 
ID3     func(2) 
ID3     func(3)

I searched for certain questions but found nothing fitting. Thanks in advance!

wolf_wue
  • 296
  • 1
  • 15

1 Answers1

0

For example

library(tidyverse)
library(magrittr)
func <- function(x) multiply_by(x, 100)
df %>% 
  separate_rows(content, convert = T) %>% 
  mutate(content = func(content))
#    name content
# 1   ID1     100
# 2   ID1     200
# 3   ID1     300
# 4   ID1     400
# 5   ID2     100
# 6   ID2     200
# 7   ID2     300
# 8   ID2     400
# 9   ID2     500
# 10  ID2     600
# 11  ID3     100
# 12  ID3     200
# 13  ID3     300
lukeA
  • 53,097
  • 5
  • 97
  • 100