Add New Variable to Data Frame Based On Other Columns in R (2 Examples) | $ Operator | transform()

preview_player
Показать описание
R code of this video:

x2 = c(3, 1, 3, 4, 2, 5))

data_new1 <- data # Duplicate example data
data_new1$x3 <- data_new1$x1 + data_new1$x2 # Add new column

data_new2 <- data # Duplicate example data
data_new2 <- transform(data_new2, x3 = x1 + x2) # Add new column

Follow me on Social Media:
Рекомендации по теме
Комментарии
Автор

Thankyou for a very clear and concise explanation. Are there any circumstances when you would prefer one method to the other?

rodneyjones
Автор

Thank you so much! This is so helpful!

anuradhanayudu
Автор

Hi! I got a question here, which I failed to find something online.
I have a huge database in which two questions are Yes/No-Questions.

Example:
Person | Q1 | Q2
1 Yes No
2 No Yes
3 Yes Yes
4 No No
5 No Yes

I want to create a dataframe which groups the participants in the possible combination of answers.
Y, Y = 1 = 20%
Y, N = 1 = 20%
N, Y = 2 = 40%
N, N = 1 = 20%

So I want to count the combination of possible answers and then add a column containing the percentage.
Does anyone have a clue how I can do this in one dataframe/table? I only was able to make three different ones and then see how many observations suit my criteria. I cannot display them yet.
Thanks in advance :-D

rdfriend
Автор

hi how would you create averages between numbers instead of adding them together

nikhilrajgopal
Автор

Hi, and thanks for your videos! I'm just approaching R and I have a very simple problem here: let's imagine that there is some missing value (NA) in x1 or x2. When summing x1 and x2, in x3 we will find a NA, as 3+NA = NA.
How can I tell R to ignore the missing value, so that 3+NA = 3 ??
I found the na.rn=TRUE function, but I don't know where to put it. Could you please give me a hint?
Thanks in advance!

Lello
Автор

nice. we can also use mutate function in dplyr package

razorscythe
Автор

Hey, great explaination
I have a question, like how can we transform data to have each statistic shown in one column?
I have a data set which requiring cleaning

rasengan
Автор

Thank you very much! I love your videos! For method 2: would it work with %>% from tidyverse? I believe we can do this with tidyverse, what would be the best method? I need to create a new df based on the collums from my actual df

larissacury