根据另一个表中的列从一个表中提取值

时间:2022-06-20 20:11:00

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

我有两个数据帧。第一个(df1)看起来像:

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

The second table (df2) looks like:

第二个表(df2)看起来像:

 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循环:

    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!

我知道我的代码语法错误。如果有人对我该怎么做有任何想法,请告诉我。谢谢!

3 个解决方案

#1


1  

using base R extraction instead of merging:

使用基本R提取而不是合并:

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

#2


2  

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

我们可以重塑第一个数据帧,然后通过第二个数据帧对其进行过滤。 df3是最终输出。

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)

#3


1  

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

#1


1  

using base R extraction instead of merging:

使用基本R提取而不是合并:

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

#2


2  

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

我们可以重塑第一个数据帧,然后通过第二个数据帧对其进行过滤。 df3是最终输出。

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)

#3


1  

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