Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to multiply a single column in a data.frame by a number

Tags:

r

I would like to know how to muliply a single column by 5 from a txt file that I used a script to read. I only know how to mulitply all of the columns by a number, but not a single column. This is my script for reading the txt file:

d = read.table(file="tst1.txt",header=TRUE)
like image 303
user2113692 Avatar asked Feb 27 '13 03:02

user2113692


1 Answers

Lets suppose your dataframe d has a column named "number" (you can see the actual names of the columns in the dataframe using str(d)). To multiply the column "number" by 5, use:

# You are referring to the column "number" within the dataframe "d"
d$number * 5

# The last command only shoes the multiplication.

# If you want to replace the original values, use
d$number <- d$number * 5

# If you want to save the new values in a new column, use
d$numberX5 <- d$number * 5

Also, try referring to the standard R documentation, which you can find in the official page.

like image 189
Oscar de León Avatar answered Sep 28 '22 02:09

Oscar de León