R 按条件合并两个CSV

R 按条件合并两个CSV,r,R,我是相对较新的两个R宇宙,我有一个问题,我必须做的任务 我有两个CSV: 第一个是植物种类的存在/不存在表。 表1(示例): 第二个是一个表,其中包含每个物种(与表1相同的物种)的指标(湿度,例如湿度)。表2(示例): 我的目标是用R和conditions“合并”这两个文件。 在表1中,如果我们看到relevé中存在一种物种(=1),则表2中的相应值给出给该单元。如果缺席(=0),则保持为0。 就像这样: species /1341/1344/1348 species1/0/0/0 specie

我是相对较新的两个R宇宙,我有一个问题,我必须做的任务

我有两个CSV:

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

第二个是一个表,其中包含每个物种(与表1相同的物种)的指标(湿度,例如湿度)。表2(示例):

我的目标是用R和conditions“合并”这两个文件。 在表1中,如果我们看到relevé中存在一种物种(=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

你知道密码吗

我们可以重复
表2中的列,以与
表1
匹配,并将它们相乘,使0的数字变成0,1的数字变成相应的
湿度

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

table1[-1]我们可以重复
table2
中的列,以与
table1
匹配,并将它们相乘,使0的数字变成0,1的数字变成相应的
湿度值

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[-1]与Ronak的回答非常相似,但使用了
左键连接

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")

与Ronak的回答非常相似,但使用
左键连接

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")

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

# 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
#加载名为tidyverse的包集合
#您可以使用install.packages(“tidyverse”)安装它
图书馆(tidyverse)
#读取数据

表1在这里,解释以注释的形式出现在代码中:

# 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
#加载名为tidyverse的包集合
#您可以使用install.packages(“tidyverse”)安装它
图书馆(tidyverse)
#读取数据

表1谢谢,但这不是我的目标。如果表1中的1取表2中相应的值,如果表1中的0,则该值保持为0。@NathanEhrstein为什么
species5
3不应该为1?@NathanEhrstein更新了答案。你能看看这是不是你想要的吗?@NathanEhrstein很高兴能帮上忙!如果您觉得投票按钮对您有用,请单击左侧投票按钮旁边的复选标记,随时进行投票。:-)每个帖子你只能接受一个答案。谢谢,但这不是我的目标。如果表1中的1取表2中相应的值,如果表1中的0,则该值保持为0。@NathanEhrstein为什么
species5
3不应该为1?@NathanEhrstein更新了答案。你能看看这是不是你想要的吗?@NathanEhrstein很高兴能帮上忙!如果您觉得投票按钮对您有用,请单击左侧投票按钮旁边的复选标记,随时进行投票。:-)每个帖子只能接受一个答案。我用我想要的结果编辑了我的问题。我用我想要的结果编辑了我的问题
# 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