如何根据R中另一个 Dataframe 中的条件向 Dataframe 添加值?

0aydgbwb  于 2023-03-10  发布在  其他
关注(0)|答案(3)|浏览(158)

我有两个 Dataframe ,并希望完成一个基于另一个。第一个 Dataframe 称为df1,有100列,A,B,C,D和c(5:100)。第二个 Dataframe 称为df2,是完全相同的,但列c(5:100)有一个不同的名称,并没有在那里的值。现在为每个唯一的值在列A,看看df1,在列D中,它是“内部”,它的值是“1”在列c(5:100),然后把“I”在 Dataframe 称为“df2”列c(5:100),如果0把“O”。我有一个例子给你,想象第一个 Dataframe 是像df1,我想有df2作为结果。

df1 <- structure(list(A=c("A_1_01", "A_1_01", "A_1_01"), B=c("A", "A", "A"), C=c("1", "1", "1"), D=c("inside", "eating", "sleeping"), "1"=c("1","1","0"), "2"=c("1","0","0"), "3"=c("0","0","1"), "4"=c("0","1","1")), class= "data.frame", row.names = c(NA,-3L))
df2<- structure(list(A=c("A_1_01", "A_1_01", "A_1_01"), B=c("A", "A", "A"), C=c("1", "1", "1"), D=c("inside", "eating", "sleeping"), "1"=c("1","1","0"), "2"=c("1","0","0"), "3"=c("0","0","1"), "4"=c("0","1","1"), "1_Location"=c("I","I", "I"), "2_Location"=c("I","I", "I"), "3_Location"=c("O","O", "O"), "4_Location"=c("O","O", "O")), class= "data.frame", row.names = c(NA,-3L))

我怎么用R来做这个呢?
谢谢大家,现在我该如何将列转换为行,以获得df3中所需的结构:

df3<- structure(list(H=c("1","2","3","4","1","2","3","4","1","2","3","4"),
                     A=c("A_1_01", "A_1_01", "A_1_01","A_1_01", "A_1_01",
                         "A_1_01","A_1_01", "A_1_01", "A_1_01","A_1_01",
                         "A_1_01", "A_1_01"),
                     B=c("A", "A", "A","A", "A", "A","A", "A", "A","A", "A", "A"),
                     C=c("1", "1", "1","1", "1", "1","1", "1", "1","1", "1", "1"),
                     D=c("inside","inside","inside","inside",
                         "eating","eating","eating","eating",
                         "sleeping","sleeping","sleeping","sleeping"),
                     Value=c(1,1,0,0,1,0,0,1,0,0,1,1),
                     Location=c("I","I","O","O","I","I","O","O","I","I","O","O")),
                class= "data.frame", row.names = c(NA,-12L))
bejyjqdl

bejyjqdl1#

您可以在所选列上使用ifelse来创建df2

library(dplyr)
df1 %>% 
  group_by(A) %>% 
  mutate(across(`1`:`4`, ~ ifelse(.x[D == "inside"] == 1, "I", "0"), .names = "{col}_Location"))

#       A B C        D 1 2 3 4 1_Location 2_Location 3_Location 4_Location
#1 A_1_01 A 1   inside 1 1 0 0          I          I          0          0
#2 A_1_01 A 1   eating 1 0 0 1          I          I          0          0
#3 A_1_01 A 1 sleeping 0 0 1 1          I          I          0          0
xzv2uavs

xzv2uavs2#

您可以从df1创建df2,如下所示。(注意,如果df2实际上是一个不同的框架,其列数1:100与df1不同[在您的示例中看起来是相同的],只需将df2替换为内连接中的第一个框架)

inner_join(
  df1, 
  df1 %>% 
    filter(D=="inside") %>%
    mutate(across(-c(1:4), ~if_else(.x==1, "I", "O"),.names = "{.col}_Location")) %>% 
    dplyr::select("A",ends_with("Location")),
  by="A"
)

输出:

A B C        D 1 2 3 4 1_Location 2_Location 3_Location 4_Location
1 A_1_01 A 1   inside 1 1 0 0          I          I          O          O
2 A_1_01 A 1   eating 1 0 0 1          I          I          O          O
3 A_1_01 A 1 sleeping 0 0 1 1          I          I          O          O
xqnpmsa8

xqnpmsa83#

使用if/else

library(dplyr)
 df1 %>% 
  mutate(across(`1`:`4`, ~ if(any(D == 'inside' & .x == 1)) "I" else "0", 
   .names = "Location_{.col}"), .by = 'A')
  • 输出
A B C        D 1 2 3 4 Location_1 Location_2 Location_3 Location_4
1 A_1_01 A 1   inside 1 1 0 0          I          I          0          0
2 A_1_01 A 1   eating 1 0 0 1          I          I          0          0
3 A_1_01 A 1 sleeping 0 0 1 1          I          I          0          0

相关问题