Reputation: 131
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)
Upvotes: 13
Views: 71384
Reputation: 2361
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.
Upvotes: 21
Reputation: 6477
d[,i]<-d[,i]*5
. See ?Extract
for more information about extracting parts of objects.
Upvotes: 5