Gambit
Gambit

Reputation: 233

How can I make this table in R?

How can I make a table like this :

enter image description here

In this table, Col_1 and Col_2 are independent variables, while Col_3 is dependent variable ( Col_1 + Col_2 = Col_3 )

Can anyone help me?

Upvotes: 0

Views: 84

Answers (3)

9314197
9314197

Reputation: 241

A tidyverse solution would be

library(tidyverse)
df <- df %>% 
   mutate(Col_3 = Col_1 + Col_2)

Where df is

df <- data.frame("Col_1" = c(2,4,5,7,6), "Col_2" = c(3,7,4,8,4))

Upvotes: 2

rg255
rg255

Reputation: 4169

Assuming you have data in a data.frame called df

df$Col_3 <- df$Col_1 + df$Col_2

Where

df <- data.frame("Col_1" = c(2,4,5,7,6), "Col_2" = c(3,7,4,8,4))

The original data.frame is made using the data.frame() function, passing two double-type (numeric) vectors, created with the c() function. The two columns are isolated as vectors using $, summed using arithmetic operator + and assigned to a new column in df using the assign operator <-.

Other ways of doing this would include the rowSums() function, mutate() in the tidyverse packages (df %>% mutate(Col_3 = Col_1 + Col_2) -> df) or using a data.table (see the data.table package: dt[, Col_3 := Col_1 + Col_2] which would be my go to).

Upvotes: 3

J_Alaniz
J_Alaniz

Reputation: 98

I'd suggest using data.table, as it would be as easy as

DT[,"Col_3":=Col_2+Col_3]

Sorry, I do not have enough rep to comment

Upvotes: 2

Related Questions