简体   繁体   English

基于r中另一个数据帧中的列向数据帧添加值

[英]Adding values to a dataframe based on columns in another dataframe in r

Hej!嘿!

I have a dataset with three columns: Value, Country and Species.我有一个包含三列的数据集:值、国家和物种。 For example a data frame that can be created like this:例如,可以像这样创建一个数据框:

Value <- c(1,2,3,4,2,6,3,5)
Country <- c("Country A", "Country A", "Country A", "Country B", "Country B", "Country B", "Country B", "Country B")
Species <- c("Species A", "Species B", "Species C", "Species A", "Species B", "Species C", "Species D", "Species E")
p <- data.frame(Value, Country, Species)

Then I also have another data frame that is empty, except for the first column.然后我还有另一个空的数据框,除了第一列。 Created like this:像这样创建:

Species2 <- levels(p$Species)
Country2 <- levels(p$Country)
x <- data.frame(Country2)
x[Species2] <- NA

And now I'm looking for a way to combine these two datasets, so that I can put the values from dataset p into the empty cells from dataset x, based on the names from the Country and the Species.现在我正在寻找一种方法来组合这两个数据集,以便我可以根据 Country 和 Species 的名称将数据集 p 中的值放入数据集 x 中的空单元格中。 So for example the Value with Country A and Species A was 1 (in dataframe p) so I want a 1 in dataframe x in the cell from the column from Species A and where the first column says Country A.例如,国家 A 和物种 A 的值是 1(在数据帧 p 中),所以我希望在来自物种 A 的列的单元格中数据帧 x 中的值为 1,并且第一列表示国家 A。

I hope this question makes sense, and that somebody could help me!我希望这个问题是有道理的,有人可以帮助我! Thank you!谢谢!

If your datasets are different you can reshape to long x and then merge with p using left_join() .如果您的数据集不同,您可以重塑为 long x ,然后使用left_join()p合并。 After that, reshape to wide.之后,重塑为宽。 Next the code with tidyverse functions:接下来是带有tidyverse函数的代码:

library(tidyverse)
#Code
newx <- x %>% pivot_longer(-Country2) %>%
  rename(Country=Country2,Species=name) %>%
  left_join(p) %>%
  mutate(value=Value) %>% select(-Value) %>% 
  pivot_wider(names_from = Species,values_from=value) %>%
  rename(Country2=Country)
 

Output:输出:

# A tibble: 2 x 6
  Country2  `Species A` `Species B` `Species C` `Species D` `Species E`
  <fct>           <dbl>       <dbl>       <dbl>       <dbl>       <dbl>
1 Country A           1           2           3          NA          NA
2 Country B           4           2           6           3           5

In base R , we can do this easilybase R ,我们可以轻松地做到这一点

xtabs(Value ~ Country + Species, p)

-output -输出

#           Species
#Country     Species A Species B Species C Species D Species E
#  Country A         1         2         3         0         0
#  Country B         4         2         6         3         5

Maybe you can try reshape which transform long data frame to a wide one也许您可以尝试reshape将长数据帧转换为宽数据帧

> reshape(p,direction = "wide",idvar = "Country",timevar = "Species")
    Country Value.Species A Value.Species B Value.Species C Value.Species D
1 Country A               1               2               3              NA
4 Country B               4               2               6               3
  Value.Species E
1              NA
4               5

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 根据R中另一个数据框中的值重命名数据框列 - Rename dataframe columns based on values in another dataframe in R 根据 R 中的另一个 dataframe 复制 dataframe 中的列 - Copy columns in a dataframe based on another dataframe in R 基于另一个数据帧重命名数据帧的列,除了 R 中不在该数据帧中的列 - Rename columns of a dataframe based on another dataframe except columns not in that dataframe in R 根据另一列中的值对 R dataframe 中的列进行分组 - group columns in R dataframe based on values in another column 如何根据另一个向量中的值删除 R 中数据帧中的列? - How to remove columns in a dataframe in R based on the values from another vector? 如何根据另一列的值聚合两列的 R dataframe - How to aggregate R dataframe of two columns based on values of another 根据数据框中的多个列向r数据框中添加新列 - Adding new columns to r dataframe based on multiple columns within the dataframe R:根据另一个数据帧的列重新调整数据帧的列 - R: relevel columns of a dataframe based on columns of another dataframe 在R中,根据行和列从另一个数据框中选择值,这些值保存在数据框中 - In R, select values from another dataframe based on rows and columns, which are saved in dataframe 基于 R 中的另一个 DataFrame 重命名列 - Rename columns based on another DataFrame in R
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM