Subsetting from multiple dataframes

Hello, new here.
I am trying to gather data from multiple dataframe.

My dataframe called "a3" is empty, and wants to copy every variable listed from the "a2" dataframe.

Next, I want to create a new variable called "fruits" in "a3" , which is the sum of 3 variables called apple, orange, and mango, into a variable called "fruits" based on dataframe "a".

The problem is that when I do this, it takes every variable listed in dataframe "a" and overrides the variables copied from "a2".
The only thing I wanted was dataframe from "a2" and the new variable based off of "a"
How should I approach this/what am I doing wrong? My attempt is below.

library(dplyr)
a <- data.frame(apple = sample(c(1), 50, rep=T), mango = sample(c(2), 50, rep=T), orange = sample(c(3), 50, rep=T))
a2 <- data.frame(shoes = sample(c("Male"), 50, rep=T), shirts = sample(c("Female"), 50, rep=T))

a3 <- a2

a3 <- a %>% rowwise() %>% mutate(fruits = sum(apple, orange, mango, na.rm = "TRUE"))

Hi @Affinity , remember put a reproducible example of data, is a better way for help you all the community.
You could make something like that: dput(a3[1:20,]) # select the firts 20 rows and all the column

added an example. Same situation where I cannot include a2 in my a3 dataframe

I solved it,
just had to change the last line.

a <- a %>% rowwise() %>% mutate(fruits = sum(apple, orange, mango, na.rm = "TRUE"))
a3$fruits <- a$fruits

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.