Apologies if this is basic question. I'm a novice. Any directions are much appreciated.
I have df1 as below (POSIXct) (135 rows)
> head(df1)
uniqueSessionsIni uniqueSessionsEnd
1 2015-01-05 15:00:00 2015-01-05 15:59:00
2 2015-01-05 15:00:00 2015-01-05 15:59:00
3 2015-01-05 15:00:00 2015-01-05 15:59:00
vector names - with names for the new 600 columns, as below.
> head(names)
[1] "m0p0" "m1p0" "m2p0" "m3p0" "m4p0" "m5p0"...
and
> head(allPairs)
Var1 Var2 names
1 1 0 m1p0
2 1 1 m1p1
I want to populate all rows of df1, columns 4 to 603 with values based on: vector names - with names for the new 600 columns, as below. uniqueSessionsIni Var1 + Var2.
You'll notice that Var1 corresponds to the digit after "m" in col. names, and Var2 corresponds to digit after "p" in names.
The result would be something like this (but with more columns).
> head(df1)
uniqueSessionsIni uniqueSessionsEnd m1p0 m1p1
1 2015-01-05 15:00:00 2015-01-05 15:59:00 2015-01-05 15:01:00 2015-01-05 15:02:00
2 2015-01-05 16:00:00 2015-01-05 15:59:00 2015-01-05 16:01:00 2015-01-05 16:02:00
3 2015-01-05 17:00:00 2015-01-05 15:59:00 2015-01-05 17:01:00 2015-01-05 17:02:00
I've tried the following code to create the new columns in df1:
df1[,names] <- NA
This successfully creates the new columns and populates with NA
So I'm trying to create a condition with a for loop to populate these new columns (3 to 603), with the code
df1[,names] <- for (i in df1$timestamps)
df1$uniqueSessionsIni + (as.posix(allPairs$Var1) + (as.posix(allPairs$Var2)
But R responds as if the expression is incomplete (+).
Is this a matter of a syntax mistake? Or I need another solution altogether to populate the new columns?
Thank you in advance.
forloops should be an exception in your daily work).2015-01-05 15:01:00not2015-01-05 15:00:00? Does theallPairsdata.frame have 135rows as well? This is easy to do, but I think the output does not reflect your data. Also, provide one more row to the allPairs data.frame to be able to work with your 3 rows.