I have created a new dataframe (called "averages") in r based on an already existing dataframe (named "old). "averages" has a column with all the unique values from a column in "old". Now I would like to add another column from "old" to "averages", with values from "old" that match the values associated with the unique values from the other column in "old".
What I used to make the new dataframe:
> averages <- data.frame(unique(old$Tree), avg) ## creating new dataframe
This was the code I tried to add the new column:
> averages <- data.frame(unique(old$Tree), avg, old$Site) ## creating new dataframe
And I get this error:
Error in data.frame(unique(old$Tree), avg, old$Site) : arguments imply differing number of rows: 50, 1110
I tried fixing the issue with this but thought it wouldn't work (it didn't):
> averages <- data.frame(unique(old$Tree), avg, unique(old$Site)) ## creating new dataframe
Error in data.frame(unique(old$Tree), avg, unique(old$Site)) : arguments imply differing number of rows: 50, 3
Any help on how to add this new column would be much appreciated!
It looks like you could do it using dplyr
install.packages("tidyverse")
library(tidyverse)
df <- left_join(averages, old, by = c("ID_col_in_averages" = "ID_col_in_old"))
where if I understand correctly both ID colums to use in the join are Tree
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.