I am having troubles understanding how to use lapply in R. I currently have a datatable in spotfire called dt. I am trying to create a list in r, where the key is the row value.
I am currently using a for loop to do it, but I know that an lapply would be better for this case.I know that lapply should speed up the process, but I am having a lot of issues of understanding how to use lapply with functions. My current code looks like this:
for(row in 1:nrow(dt)){
index[[toString(row)]]<-unlist(c(unlist(value)[row],' ',0,' ',Inf))
}
Can someone help me write up the lapply version of this and explain the steps and how to think of programming it. Even if this won't improve the speed of the code greatly, since I have some bigger for loops that i want to convert, but I want to start off with something simple. Thank you
For a data table you can use this
DT = data.table(
ID = c("b","b","b","a","a","c"),
a = 1:6,
b = 7:12,
c = 13:18
)
Since I am new to R, I will write the list that I want as a python dictionary. I at the end I want to have some to look like this:
{
"1":[1,' ',0,' ',Inf],
"2":[2,' ',0,' ',Inf],
...,
"6":[3,4,' ',0,'',Inf]
}
The values inside the arrays dont matter as much, it just needs to be arrays.