[英]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 easily在
base 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.