简体   繁体   中英

Extract Values from one table based on columns in another table

I have two data frames. The first one (df1) looks like:

Item       Col1        Col2        Col3         
 A         Food        Fruit       Apple        
 B         Food        Veggie      NA
 C         xxx          yyy        zzz   

The second table (df2) looks like:

 Name        Number
Apple         Col3
Veggie        Col2

I want to get a final table as below:

Item        Name        Number
 A          Apple        Col3
 B          Veggie       Col2

I've tried to use a for loop as:

    for (i in 1:nrow(df2)){ 
        new_df <- subset(df1, df2[i,1] %in% df1$df2[,2])
        print(new_df)
}

I know the syntax is wrong for my code. If anyone has any ideas on what should I do, Please let me know. Thank you!

We can reshape the first data frame and then filter it by the second data frame. df3 is the final output.

library(tidyverse)

df3 <- df1 %>%
  gather(Number, Name, -Item) %>%
  semi_join(df2, by = c("Name", "Number")) %>%
  select(Item, Name, Number) %>%
  arrange(Item)
df3
#   Item   Name Number
# 1    A  Apple   Col3
# 2    B Veggie   Col2

DATA

df1 <- read.table(text = "Item       Col1        Col2        Col3         
 A         Food        Fruit       Apple        
 B         Food        Veggie      NA
 C         xxx          yyy        zzz ",
                  header = TRUE, stringsAsFactors = FALSE)


df2 <- read.table(text = " Name        Number
Apple         Col3
Veggie        Col2",
                  header = TRUE, stringsAsFactors = FALSE)

If you want to do it with a loop, you could do it like that:

DATA

df1 = data.frame(Item=c("A","B"), Col1 = "Food", Col2 = c("Fruit", "Veggie"), 
  Col3 = c("Apple",NA), stringsAsFactors = F)
df2 = data.frame(Name  = c("Apple", "Veggie"), Number = c("Col3", "Col2"),
  stringsAsFactors = F)

SOLUTION WITH LOOP

new_df = df2
for(i in 1:nrow(new_df)){
  new_df$Item[i] = df1[which(df1[[df2$Number[i]]] == df2$Name[i]),"Item"]
}
new_df

using base R extraction instead of merging:

cbind(df1[1],Name=df1[cbind(1:nrow(df1),match(df2$Number,names(df1)))],df2[2])
  Item   Name Number
1    A  Apple   Col3
2    B Veggie   Col2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM