简体   繁体   English

R:在dataframe中手动将特定行和列除以数字

[英]R: Manually divide a specific row and column by a number in a dataframe

Unfortunately, I could only come across questions on how to divide columns by a specific number.不幸的是,我只能遇到有关如何按特定数字划分列的问题。 However, I would like to divide the value only in a specific row and column by a number.但是,我只想将特定行和列中的值除以一个数字。

To illustrate, let's use the following data:为了说明,让我们使用以下数据:

structure(list(Name = c("A", "B", "C"), Return = c(0.1, 0.5, 
0.6), Size = c(100000000000, 40030202002, 405045000000)), class = "data.frame", row.names = c("Europe", 
"Asia", "USA"))

Now how can I divide the number in row "Europe" and column "Size" by 1 million?现在如何将“Europe”行和“Size”列中的数字除以 100 万?

I know for the whole column you can do that:我知道对于整个专栏你都可以这样做:

Data$Size <- Data$Size /1000000

But how can I limit it only to a certain row with a given row name?但是我怎样才能将它限制为具有给定行名的特定行呢?

We can use row/column names to subset and divide我们可以使用行/列名称来子集和划分

Data["Europe", "Size"] <- Data["Europe", "Size"]/1000000

-testing -测试

> Data$Size
[1] 1.00000e+11 4.00302e+10 4.05045e+11
> Data["Europe", "Size"] <- Data["Europe", "Size"]/1000000
> Data$Size
[1] 1.00000e+05 4.00302e+10 4.05045e+11

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM