I am trying to store vector of variable length in a new column of an existing dataframe.
My initial dataframe-
data -
job_id usetime
abc 2345
abc1 4353
jsdf 34985
I have a numeric vector(indices_excluded) containing the indices from the dataframe. Using these numbers i have to extract the usetimes of the corresponding indices.
I want to store all of these usetimes corresponding to the row indices in a new column called "runtime_excluded"
To do so i tried running this code in a loop (applied on i)
data[i, "runtime_excluded"]<-I(list(data[indices_excluded, "USETIME"]))
The numeric vector "indices_excluded" keeps changing on each iteration.
This is give me a warning saying
value = list( : replacement element 1 has 2 rows to replace 1 rows
It is storing only the first element of the list.
I want to store all the usetimes in that dataframe element.
Desired output-
data -
job_id usetime runtime_excluded
abc 2345 234,4325
abcd 4353 2435
abcde 34985 2134, 234234, 34223
I came across a few relevant questions like one,two,three but could find an answer to my problem.
EDIT-
My initial dataframe-
data -
job_id starttime endtime endtime_modified usetime
abc 1 23 20 22
abc1 2 15 13 13
jsdf 30 40 39 10
The code that im running -
k=nrow(data)
for(i in 1:k)
{
indices_peak<-which((data[i,"endtime"] >= data$starttime)
& (data[i,"endtime"] <= data$endtime)
indices_peak95<-which((data[i,"endtime_modified"] >= data$starttime)
& (data[i,"endtime_modified"] <= data$endtime_modified)
indices_excluded<-indices_peak[!indices_peak %in% indices_peak95]
data[i,"peak"]<-length(indices_peak)
data[i,"peak_95"]<-length(indices_peak95)
data$runtime_excluded[i]<-data[indices_excluded, "USETIME"]
}
Desired output-
job_id starttime endtime endtime_modified usetime peak peak_95 runtime_excluded
abc 1 24 22 22 2 2 20
abc1 2 24 20 22 2 3 -
jsdf 3 23 23 9 3 1 22,20
Starttimes and endtimes are in seconds and are in referene to a particular time.