0

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.

  • 1
    Hi, could you please provide a [reproductible example](https://stackoverflow.com/a/5963610/2414988) (ie. some data to run your code) so that we can better help you? – Biblot Oct 18 '19 at 08:56
  • 2
    That `lapply` is faster than a `for` loop is basically a myth. It's just marginally true in the best scenarios. Vectorization, on the other hand, really speeds things up. Whether your code is vectorizable we canot know without having a reproducible example and a clear statement of your goal. – nicola Oct 18 '19 at 08:59
  • @nicola I am not sure how to add the datatable I am using, since I dont see here anywhere where I can add files. I pretty much just want a list where the key is the row value of the data table. So for example if i have a data table with 3 rows, I want a list where index[1]=[1,' ',0,Inf], index[2]=[5,' ',0,Inf],index[3]=[2.' ',0,Inf ]. The first value, was determined based off of a list that I previously created. – Nikita Belooussov Oct 18 '19 at 09:05
  • 1
    @NikitaBelooussov You don't have to share your entire data frame, only a sample will be enough. See my link to create a minimal dataset. – Biblot Oct 18 '19 at 09:18
  • 1
    Ok for the input `data.table`. What's your desired output? – nicola Oct 18 '19 at 09:34
  • @nicola, i just want a list, where the key is the row number of the datatable, and links to a vector, that is created. It might help that I am thinking of the list as a python dictionary. So I want a dictionary where the key links to an array that was created. – Nikita Belooussov Oct 18 '19 at 09:38
  • If you could also provide a reproducible example of your preferred output list, helping you is a lot easier. – JDG Oct 18 '19 at 09:44
  • @J.G. i added an example please tell me if something is unclear – Nikita Belooussov Oct 18 '19 at 09:50
  • @NikitaBelooussov I'm sorry, I can't seem to understand the relation between your input and output, could you give some more details? What does the `value` variable contain in your code? Also, you are using `unlist` on a vector: `unlist` is used to simplify a `list` into a vector. – Biblot Oct 18 '19 at 09:53
  • @SamuelDiebolt sorry the i=i+1 was left over from something else and I forgot to remove it. just a list where the key is the row number of the data table and it links to a array. You can give me and array of [1] each time it is fine. Sorry if im using the incorrect terminology im thinking of this as a python dictionary – Nikita Belooussov Oct 18 '19 at 09:58

1 Answers1

0

I still don't understand exactly the relationship between the input DT and your output. You could create a list of size equal to the number of rows in DT and containing vectors similar to what you have in your output like this:

lapply(1:nrow(DT), function(x) c(x, " ", 0, " ", Inf))
# [[1]]
# [1] "1"   " "   "0"   " "   "Inf"
#
# [[2]]
# [1] "2"   " "   "0"   " "   "Inf"
#
# [[3]]
# [1] "3"   " "   "0"   " "   "Inf"
#
# [[4]]
# [1] "4"   " "   "0"   " "   "Inf"
#
# [[5]]
# [1] "5"   " "   "0"   " "   "Inf"
# 
# [[6]]
# [1] "6"   " "   "0"   " "   "Inf"

I think this might be similar to a python dictionary. The names attribute of this list would be your keys in a python dictionary.

Biblot
  • 695
  • 3
  • 18