根据条件合并两个CSV文件

时间:2020-04-29 07:29:51

标签: r

我是相对较新的两个R宇宙,我在执行任务时遇到了问题。

我有两个CSV:

第一个是带有植物种类的存在/不存在表。 表1(示例):

species /1341/1344/1348
species1/0/0/0
species2/0/1/0
species3/0/0/0
species4/1/0/0
species5/0/0/1
species6/0/0/0

0 = absence in the vegetation relevé 
1 = presence in the vegetation relevé 
1341,1344,1348 = vegetation relevé ID

第二个是一张表,其中包含每个物种(与表1相同的物种)的指标(Humidité_édphique,例如谦卑)。表2(示例):

species /humidity
species1/3
species2/2
species3/7
species4/4
species5/1
species6/3

我的目标是使用条件将R与两个文件“合并”。 在表1中,如果我们看到关联中存在某个物种(= 1),则将表2中的相应值赋给该单元格。如果不存在(= 0),则保持为0。 像这样:

species /1341/1344/1348
species1/0/0/0
species2/0/2/0
species3/0/0/0
species4/4/0/0
species5/0/0/1
species6/0/0/0

对代码有任何想法吗?

3 个答案:

答案 0 :(得分:0)

我们可以重复table2中的列以与table1匹配并将它们相乘,以使0的数字变为0,而1的数字转向其对应的humidity值。

table1[-1] <- table1[-1] * table2[rep(2, ncol(table1) - 1)]

table1
#   species 1341 1344 1348
#1 species1    0    0    0
#2 species2    0    2    0
#3 species3    0    0    0
#4 species4    4    0    0
#5 species5    0    0    1
#6 species6    0    0    0

答案 1 :(得分:0)

与Ronak的回复非常相似,但使用的是left_join

library(dplyr)
library(tibble)

table1 <- tibble(species = c("species1", "species2", "species3", "species4", "species5", "species6"),
               A1341 = c(0, 0, 0, 1, 0, 0),
               A1344 = c(0, 1, 0, 0, 0, 0),
               A1348 = c(0, 0, 0, 0, 1, 0))


table2 <- tibble(species = c("species1", "species2", "species3", "species4", "species5", "species6"),
                 humidity = c(3, 2, 7, 4, 1, 3))

table3 <- table1 %>%
  left_join(table2, by = c("species" =  "species"))

# or this will work too:
table3 <- left_join(table1, table2, by = "species")

对于table3,结果如下:

# A tibble: 6 x 5
  species  A1341 A1344 A1348 humidity
  <chr>    <dbl> <dbl> <dbl>    <dbl>
1 species1     0     0     0        3
2 species2     0     1     0        2
3 species3     0     0     0        7
4 species4     1     0     0        4
5 species5     0     0     1        1
6 species6     0     0     0        3

答案 2 :(得分:0)

在这里,说明在代码中以注释的形式出现:

# loading a collection of packages called the tidyverse
# you can install it with install.packages("tidyverse")
library(tidyverse)

# Read data
table1 <- read_delim(file = "data/table1.txt", delim = "/")
table2 <- read_delim(file = "data/table2.txt", delim = "/")

# clean the column names
# you can get the janitor package with
# install.packages("janitor")
table1 <- janitor::clean_names(table1)
table2 <- janitor::clean_names(table2)

# turn it into tidy data
# see https://r4ds.had.co.nz/tidy-data.html for a definition and the "why
table1_tidy <- table1 %>% 
  pivot_longer(cols = c(-species),
               names_to = "id",
               values_to = "presence")

# then we combine the two tables based on columns they have
# in common, this is also called a "join"
combined_table <- left_join(table1_tidy, table2)

# now we multiply the indicator value with the presence or absence column
result_table <- combined_table %>% 
  mutate(value = presence * humidity)

# the resulting table is quite nice to work with,
# but in case you want it in the format you specified
# in your post, we need to make it wider again:
wide_result_table <- result_table %>% 
  select(species, id, value) %>% 
  pivot_wider(names_from = id,
              values_from = value)

wide_result_table