R-squared only specific columns

Given a dataset with a timestamp and several columns of data following it

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 

I would like a square of columns V1-V3 giving me this

 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 

As for right now, I'm just using

 data_sq<-data^2 

to get the square root. But it destroys the timestamp, so I have to return it with something really uncomfortable, for example

 data_sqd<-cbind(data_sq,data$hour) 

I tried

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

but i'm also losing timestamp

So, how do I save an immutable timestamp column and specify which columns I want to use? I apologize for the lack of reproducible data, but I hope this is a fairly simple question that I can handle it :)

+4
source share
3 answers
 data_sq <- data # try not to use data, it a function in base data_sq[,2:4] <- data[,2:4]^2 # subsetting was off but you were close! 
+7
source

One-step solution:

 data_sq <- transform(data, V1=V1^2, V2=V2^2, V3=V3^2) 
+7
source

You need to replace only 2: 4 columns, not the entire object; eg:.

 data_sq[, 2:4] <- data[, 2:4]^2 
+4
source

Source: https://habr.com/ru/post/1416685/


All Articles