I have got a list with 10 data.frames, each one with 3 columns. I need to create a new column for each data.frame with letters. These letters have to be assigned conditionally on a specific data.frame column.
Here one of my data.frame within the list:
[[10]]
x y z
1 24 20 15
2 25 15 25
3 25 14 20
The column from which the new values have to be assigned is z
. So lets say that 15 = S, 25 = A, 20 = T.
My data.frame with the new column should be:
[[10]]
x y z new_col
1 24 20 15 S
2 25 15 25 A
3 25 14 20 T
UPDATE
Here what I am trying with my real data (following @csgillespie suggestion):
lookup <- data.frame(num = c(-5, -1:8, 13:20, 25:32), let = c('P', 'A', 'E', 'AE', 'ASE', 'AS', 'ASW',
'ZQ', 'AW', 'AN', 'OQ', 'AA', 'RT', 'SE',
'S', 'QP', 'W', 'NW', 'N', 'C', 'YE', 'PP',
'ZQ', 'CS', 'L', 'CW', 'UW', 'CN'))
lst <- lapply(lst, function(i)
cbind(i, new_col=lookup[i$x, 2]))
But I got the following error, probably due to the negative numbers on the lookup data.frame:
Error in
[.default
(xj, i) : only 0's may be mixed with negative subscripts
2nd UPDATE
Thanks @m-dz!
Job done :)