Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
357 views
in Technique[技术] by (71.8m points)

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

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)
See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...