gpt4 book ai didi

r - R-仅对特定列进行平方

转载 作者:行者123 更新时间:2023-12-04 13:14:26 27 4
gpt4 key购买 nike

给定一个带有时间戳的数据集,以及紧随其后的几列数据

data=
hour V1 V2 V3
1 2012-01-19 08:00:00 0.04551064 0.002851064 0.01842553
2 2012-01-19 09:00:00 -0.05305000 -0.052266667 -0.07455000
3 2012-01-19 10:00:00 -0.07518333 -0.101333333 -0.10670000
4 2012-01-19 11:00:00 -0.09195000 -0.099383333 -0.11176667
5 2012-01-19 12:00:00 -0.07743333 -0.074000000 -0.09106667
6 2012-01-19 13:00:00 -0.10978333 -0.096200000 -0.11343333

我想将V1-V3列平方给我
data_sq=
hour V1 V2 V3
1 2012-01-19 08:00:00 0.002071218 8.128565e-06 0.0003395002
2 2012-01-19 09:00:00 0.002814303 2.731804e-03 0.0055577025
3 2012-01-19 10:00:00 0.005652534 1.026844e-02 0.0113848900
4 2012-01-19 11:00:00 0.008454803 9.877047e-03 0.0124917878
5 2012-01-19 12:00:00 0.005995921 5.476000e-03 0.0082931378
6 2012-01-19 13:00:00 0.012052380 9.254440e-03 0.0128671211

至于现在我只用
data_sq<-data^2

得到平方根。但是它破坏了时间戳,所以我不得不将它放回去,但确实有些尴尬,例如
data_sqd<-cbind(data_sq,data$hour)

我试过
data_sq<-(data[,c(2:4)]^2

但我也松开了时间戳

那么,如何保持我的时间戳列完整并指定要平方的列?对于缺乏可复制的数据我深表歉意,但我希望这是一个可以解决的足够简单的问题:)

最佳答案

data_sq <- data # try not to use data, it's a function in base
data_sq[,2:4] <- data[,2:4]^2 # subsetting was off but you were close!

关于r - R-仅对特定列进行平方,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10940013/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com