You can achieve this by using the ifelse function along with mutate from the dplyr package in R. Here is a sample code snippet that does this:
main.r454 chars19 lines
In this code, lengths(values) is used to check the length of each list in the values column. If the length is 0 (i.e., the list is empty), it assigns NA to the new columns. Otherwise, it uses sapply to extract the first and second elements of each list.
If you have a list with more than two elements and you want to create new columns for all of them, you can use a loop to generate the new columns dynamically:
main.r511 chars22 lines
This code first finds the maximum length of the lists in the values column. Then it uses a loop to generate new columns for each position up to the maximum length. If a list is shorter than the current position, it assigns NA to the corresponding new column. Otherwise, it extracts the value at the current position from each list.
gistlibby LogSnag