Reputation: 2223
what I have dataframe that looks like this
df>
col1 col2
1 c('a', 'b', 'c')
2 c('r', 't')
3 c('x', 'y')
What I want a dataframe that looks like this
df>
col1 col2 col3
1 c('a', 'b', 'c') 'c'
2 c('r', 't') 't'
3 c('x', 'y') 'y'
Upvotes: 0
Views: 49
Reputation: 4233
Here is an example:
df <- data.frame(C = I(list(list("a","b"),list("a","b","c"))))
df$D <- sapply(df$C, function(x) x[length(x)])
Output
C D
1 a, b b
2 a, b, c c
Upvotes: 1
Reputation: 4130
Try this
df$col3 <- sapply(df$col2,function(s) as.vector(s)[length(s)])
Upvotes: 1