Indexing another data frame in R -


you have 1 data frame class of animal:

animal <- data.frame('code' = c(1:4), 'class' = c('mammal','bird','fish','lizard')) 

output:

code  animal   1    mammal   2     bird   3     fish   4    lizard 

you have data frame showing land speed of animals based on code:

land.speed <- data.frame('speed' = c('42','32','6','0','100'), 'code' = c(1,2,4,3,1)) 

output:

speed code   42    1   32    2    6    4    0    3  100    1 

what create new column in second data frame titled 'animal' based on code value first data frame.

example:

speed code animal   42    1  mammal   32    2   bird    6    4  lizard    0    3   fish  100    1  mammal 

any appreciated.

animal <- data.frame('code' = c(1:4), 'class' = c('mammal','bird','fish','lizard')) land.speed <- data.frame('speed' = c('42','32','6','0','100'), 'code' = c(1,2,4,3,1)) out <- merge(land.speed, animal, = "code", all.x = true) out #   code speed  class # 1    1    42 mammal # 2    1   100 mammal # 3    2    32   bird # 4    3     0   fish # 5    4     6 lizard 

what takes land.speed data frame , merges on animal dataframe matching code variable. assuming simplified version of real data frames, , therefore more columns sure included, should by.x, by.y, all.x, all.y, , all parameters of merge fit specific needs.


Comments

Popular posts from this blog

authentication - Mongodb revoke acccess to connect test database -

r - Update two sets of radiobuttons reactively - shiny -

ios - Realm over CoreData should I use NSFetchedResultController or a Dictionary? -