I have a dataframe which is a presence absence matrix of species found in different locations (1 indicates presence, 0 absence) like so:
df1<-data.frame(replicate(5,sample(0:1,5,rep=TRUE)))
row.names(df1)<-c("location.1","location.2","location.3","location.4","location.5")
names(df1)<-c("species.1","species.2","species.3","species.4","species.5")
species.1 species.2 species.3 species.4 species.5
location.1 0 1 0 1 1
location.2 1 0 0 1 0
location.3 0 0 1 0 0
location.4 1 0 1 0 1
location.5 0 0 1 0 0
I have a second dataframe which has values for each species, like so:
df2<-c(2,4,6,8,10)
df2<-as.matrix(df2)
row.names(df2)<-c("species.1","species.2","species.3","species.4","species.5")
[,1]
species.1 2
species.2 4
species.3 6
species.4 8
species.5 10
I would like to replace all 1s in the first dataframe, on a column by column basis, with the matching value in the second dataframe, based on the species names. Producing this:
species.1 species.2 species.3 species.4 species.5
location.1 0 4 0 8 10
location.2 2 0 0 8 0
location.3 0 0 6 0 0
location.4 2 0 6 0 10
location.5 0 0 6 0 0
I have no idea how I'd go about doing this though, and can't find any similar examples online.
Does anyone have any advice?
df1*df2[,1][col(df1)]orsweep(df1, 2, df2[,1], "*")ormapply("*", df1, df2[,1])