R:将列乘以数据表中的常量 [英] R: Multiplying columns by a constant in a data table
问题描述
我尝试更正我的资料表,因此我的资料栏有相同的单位。这是我的一个例子。
I am trying to correct my data table so my columns have the same units. Here's an example of what I have.
hh:mm A V W kA V kW A kV kW
11:00 13.84 470.16 6509.88 14.89 467.85 6964.38 15.74 464.01 7303.13
11:05 12.54 475.17 5959.22 13.40 474.52 6358.89 13.34 473.13 6311.80
11:10 9.73 476.20 4632.14 10.36 473.38 4905.86 10.38 472.73 4907.14
11:15 9.20 479.30 4410.89 9.65 482.79 4659.67 9.73 479.09 4659.33
11:20 11.28 482.22 5437.78 12.03 484.95 5835.33 12.24 476.36 5829.44
11:25 11.66 481.64 5614.56 12.76 479.95 6124.56 12.88 476.86 6139.33
11:30 10.38 475.13 4934.00 11.99 480.96 5760.44 11.50 478.77 5515.13
正如你所看到的,有些列是A,V或W,而其他是kA,kV或kW。我想要做的是通过将kA,kV和kW列乘以1000来将所有这些更改为相同的单位。这是我想要得到的
As you can see, some columns are in "A","V", or "W", while others are in "kA","kV" or "kW". What I am trying to do is to change all these to the same units by multiplying the "kA", "kV" and "kW" columns by 1000. Here's what I want to get
hh:mm A V W A V W A V W
11:00 13.84 470.16 6509.88 14890 467.85 6964380 15.74 464010 7303130
11:05 12.54 475.17 5959.22 13400 474.52 6358890 13.34 473130 6311800
11:10 9.73 476.20 4632.14 10360 473.38 4905860 10.38 472730 4907140
11:15 9.20 479.30 4410.89 9650 482.79 4659670 9.73 479090 4659330
11:20 11.28 482.22 5437.78 12030 484.95 5835330 12.24 476360 5829440
11:25 11.66 481.64 5614.56 12760 479.95 6124560 12.88 476860 6139330
11:30 10.38 475.13 4934.00 11990 480.96 5760440 11.50 478770 5515130
我试过这样做:
units<-which(names(dt)=="kA") # Gives me a vector with the positions needed.
dt[,units:=units*1000] #Multiplies the vector by 1000
names(dt) <- gsub("kA", "A", names(dt)) # Changes "kA" to "A"
units<-which(names(dt)=="kV") # Gives me a vector with the positions needed.
dt[,units:=units*1000] #Multiplies the vector by 1000
names(dt) <- gsub("kV", "V", names(dt)) # Changes "kV" to "V"
units<-which(names(dt)=="kW") # Gives me a vector with the positions needed.
dt[,units:=units*1000] #Multiplies the vector by 1000
names(dt) <- gsub("kW", "W", names(dt)) # Changes "kW" to "W"
在我的第二行,我收到此错误:
On my second line, I receive this error:
Warning message:
In `[.data.table`(x2, , `:=`(units, units * 1000)) :
Supplied 48 items to be assigned to 286 items of column 'units' (recycled leaving remainder of 46 items).
任何人都可以帮助我使用我的语法?
Can anyone help me with my syntax please?
PS:这是我的输入的输入,它看起来有点滑稽,虽然。
PS: Here's my dput for the input, it looks a little funny though.
> dput(c)
structure(list(`hh:mm` = c("11:00", "11:05", "11:10", "11:15",
"11:20", "11:25", "11:30"), A = c(13.84, 12.54, 9.73, 9.2, 11.28,
11.66, 10.38), V = c(470.16, 475.17, 476.2, 479.3, 482.22, 481.64,
475.13), W = c(6509.88, 5959.22, 4632.14, 4410.89, 5437.78, 5614.56,
4934), kA = c(14.89, 13.4, 10.36, 9.65, 12.03, 12.76, 11.99),
V = c(467.85, 474.52, 473.38, 482.79, 484.95, 479.95, 480.96
), kW = c(6964.38, 6358.89, 4905.86, 4659.67, 5835.33, 6124.56,
5760.44), A = c(15.74, 13.34, 10.38, 9.73, 12.24, 12.88,
11.5), kV = c(464.01, 473.13, 472.73, 479.09, 476.36, 476.86,
478.77), kW = c(7303.13, 6311.8, 4907.14, 4659.33, 5829.44,
6139.33, 5515.13)), .Names = c("hh:mm", "A", "V", "W", "kA",
"V", "kW", "A", "kV", "kW"), row.names = c(NA, -7L), class = c("data.table",
"data.frame"), .internal.selfref = <pointer: 0x00000000003b0788>)
推荐答案
你不是真的使用 data.table
正确。这里有一种方法可以使用:
You aren't really using data.table
correctly. Here is one approach that can be used:
DT[, c("A2", "W2", "V2") := lapply(.SD, function(x) x*1000), .SDcols=c("kA", "kW", "kV")]
DT[, c("kA", "kW", "kV") := NULL]
DT
# hh.mm A V W V.1 A.1 kW.1 A2 W2 V2
# 1: 11:00 13.84 470.16 6509.88 467.85 15.74 7303.13 14890 6964380 464010
# 2: 11:05 12.54 475.17 5959.22 474.52 13.34 6311.80 13400 6358890 473130
# 3: 11:10 9.73 476.20 4632.14 473.38 10.38 4907.14 10360 4905860 472730
# 4: 11:15 9.20 479.30 4410.89 482.79 9.73 4659.33 9650 4659670 479090
# 5: 11:20 11.28 482.22 5437.78 484.95 12.24 5829.44 12030 5835330 476360
# 6: 11:25 11.66 481.64 5614.56 479.95 12.88 6139.33 12760 6124560 476860
# 7: 11:30 10.38 475.13 4934.00 480.96 11.50 5515.13 11990 5760440 478770
注意:
- 您当前的解决方案已创建一个
data.table
名称。不好的做法。我错误地不在这里转换你的一个列,但我会离开这个练习更新你的答案。 - 如果你打算使用
data.table
,熟悉set *
的一组函数。
- Your current solution has you creating a
data.table
with duplicated names. Bad practice. I made the mistake of not converting one of your columns here, but I'll leave that exercise of updating the answer to you. - If you are going to use
data.table
, get familiar with theset*
set of functions.
或者使用设置
:
cols <- grep("^k", names(dt))
for (j in cols) {
set(DT, i=NULL, j=j, value=DT[[j]]*1000)
}
# change names with `setnames` now.
setnames(DT, cols, gsub("^k", "", names(dt)[cols])
虽然我同意@Ananda不使用重复的名称。
Although I agree with @Ananda on not using duplicate names.
这篇关于R:将列乘以数据表中的常量的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!