SJUNLEE
SJUNLEE

Reputation: 167

How to calculate variables from several tables to generate a new column?

I have two data set as below. (It was made by example data.)

enter image description here

Now, I would like to calculate variables from two tables to generate a new variable.

enter image description here

As the original data is much bigger than this example, I thought "for statement" or "SQL" should be used to do this.

On the other hand, I have no idea how to conduct this calculation exactly.

Please let me know.

Thanks advance.

P.S. This is my code to make example data.

install.packages("randomForestSRC")
library(randomForestSRC)

data(pbc)

dat <- na.omit(pbc)

n.dat <- dat[,5:8]

var <- c(names(n.dat))
OR <- c(1.45, 2.68, 1.11, 1.90)

m <- data.frame(var, OR)

Upvotes: 0

Views: 65

Answers (1)

Rahul Varma
Rahul Varma

Reputation: 550

you can solve this issue by using dplyr package in R you can use following code

install.packages("dplyr")
library(dplyr)
m  <- m %>% mutate(newvar = ((sex*1.45)+(ascites*2.68)+(hempatom * 1.11)+(spider*1.90))
.

using mutate function we can create a new column

Upvotes: 1

Related Questions