jpinelo
jpinelo

Reputation: 1454

create and fill columns in dataframe in R

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.

Upvotes: 1

Views: 4389

Answers (1)

LyzandeR
LyzandeR

Reputation: 37879

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

Upvotes: 1

Related Questions