1

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.

6
  • This is not clear. It seems like you only need one column the way you say it. You need to make your example reproducible i.e. provide some real data and show us what you want the output to be (for less columns than 603) Commented Mar 3, 2015 at 16:25
  • In R creating and filling a data.frame column usually should be one step. Also, R is case sensitive and parentheses must match. Study the documentation of functions you want to use and take advantage of vectoriziation (using for loops should be an exception in your daily work). Commented Mar 3, 2015 at 16:43
  • Thank you for feedback. I've edited as suggested by @LyzandeR. Commented Mar 3, 2015 at 18:41
  • Roland, Thank you for the tips. I have read the documentation, just couldn't figure it out yet. You suggest that I try to create and populate the columns in one action. Thats is exactly what I'm trying to do. Commented Mar 3, 2015 at 18:47
  • Var2 is 0 for the first row. Is this a typo? Why is 2015-01-05 15:01:00 not 2015-01-05 15:00:00? Does the allPairs data.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. Commented Mar 3, 2015 at 20:24

1 Answer 1

1

You can try this:

Data:

df1 <- data.frame(uniqueSessionsIni=as.POSIXlt(c('2015-01-05 15:00:00','2015-01-05 16:00:00', '2015-01-05 17:00:00 ')),
                  uniqueSessionsIni=as.POSIXlt(c('2015-01-05 15:59:00','2015-01-05 16:59:00', '2015-01-05 17:59:00 ')))

#note that the names column below should be of character class and not factor
allPairs <- data.frame(Var1=c(1,1), Var2=c(0,1), names=c('m1p0','m1p1'),stringsAsFactors=F)

Solution:

#the list below creates the columns you need
mylist <- list()
for (i in 1:nrow(allPairs)){
  mylist[[allPairs[i, 3]]] <- df1$uniqueSessionsIni + 60*as.numeric(allPairs[i, 1]) + 60*as.numeric(allPairs[i, 2])
}

> mylist
$m1p0
[1] "2015-01-05 15:01:00 GMT" "2015-01-05 16:01:00 GMT" "2015-01-05 17:01:00 GMT"

$m1p1
[1] "2015-01-05 15:02:00 GMT" "2015-01-05 16:02:00 GMT" "2015-01-05 17:02:00 GMT"
#cbind all df1 and the new column from the loop
cbind(df1, data.frame(mylist))

Output:

> cbind(df1, data.frame(mylist))
    uniqueSessionsIni uniqueSessionsIni.1                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 16:59:00 2015-01-05 16:01:00 2015-01-05 16:02:00
3 2015-01-05 17:00:00 2015-01-05 17:59:00 2015-01-05 17:01:00 2015-01-05 17:02:00
Sign up to request clarification or add additional context in comments.

1 Comment

You are welcome :). I had made a small mistake, creating the wrong data. I fixed it now. Happy to have helped :) !!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.