B C
B C

Reputation: 318

Indexing another data frame in R

You have one data frame for 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 another data frame showing the land speed of the animals based on the 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 I would like to do is create a new column in the second data frame titled 'animal' based on the code value from the first data frame.

Example:

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

Any help would be greatly appreciated.

Upvotes: 0

Views: 91

Answers (3)

akrun
akrun

Reputation: 887118

We can use match. It should be faster

land.speed$animal <- animal$class[match(land.speed$code, animal$code)]
land.speed
#  speed code animal
#1    42    1 mammal
#2    32    2   bird
#3     6    4 lizard
#4     0    3   fish
#5   100    1 mammal

Or if we have matching 'code' elements in both datasets

land.speed$animal <- animal$class[land.speed$code]

Upvotes: 1

Prateek Joshi
Prateek Joshi

Reputation: 4067

You can use merge function

result<-merge(x=animal,y=land.speed,by="code",all=TRUE)

Output:

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

Upvotes: 1

tblznbits
tblznbits

Reputation: 6778

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, by = "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 this does is takes your land.speed data frame and merges on the animal dataframe by matching the code variable. Assuming that this is a simplified version of your real data frames, and therefore more columns are sure to be included, you should look into the by.x, by.y, all.x, all.y, and all parameters of merge to fit your specific needs.

Upvotes: 3

Related Questions