Add a new column from an existing column with a calculation from the previous column

R Programming Language / Data Frames in R Language

278

Program:

# input code

Student <- data.frame(
RollNo = 1:10,  
Marks = c(10, 20, 30, 40, 50, 60, 70, 80, 90, 95), 
PassOrFail = c(F, F, F, F, F, T, T, T, T, T))

# code for output

> # add marks to each student and creating a new column
> Student$NewMarks <- Student$Marks + 5

Output:

data frame, r language
> str(Student)
'data.frame':	10 obs. of  5 variables:
 $ RollNo     : int  1 2 3 4 5 6 7 8 9 10
 $ Marks      : num  10 20 30 40 50 60 70 80 90 95
 $ PassOrFail : logi  TRUE TRUE TRUE TRUE TRUE TRUE ...
 $ StudentName: chr  "Rumman" "Inza" "Jaman" "Azam" ...
 $ NewMarks   : num  15 25 35 45 55 65 75 85 95 100

Explanation:

Here, in this case, we will add a column from the existing column. We will add 5 marks of each student for the wrong question. Extra marks will be added for each student. We will create a new column NewMarks.

This Particular section is dedicated to Programs only. If you want learn more about R Programming Language. Then you can visit below links to get more depth on this subject.