Selecting delimited data in one column of data.table based on positional values in another

StackOverflow https://stackoverflow.com/questions/23673937

  •  23-07-2023
  •  | 
  •  

Question

I have a data table

x<-data.table(col1=rep("hello,how,are,you",4),col2=1:4)
              col1 col2
1: hello,how,are,you    1
2: hello,how,are,you    2
3: hello,how,are,you    3
4: hello,how,are,you    4

now I need to select values from col1 based on the values in col2. How can I do this within j of data.table?

Final data should look something like this

                 col1 col2   col3
1: hello,how,are,you    1    hello
2: hello,how,are,you    2    how
3: hello,how,are,you    3    are
4: hello,how,are,you    4    you
Was it helpful?

Solution

I don't know if this is the most efficient (is speed or memory the top concern?) solution:

library(data.table)
x <- data.table(col1=c(rep("hello,how,are,you",3), 
                       "Lirum, Larum, Löffel, Stiel"),
                col2=1:4)
x[, ind := .I]
x[, col3 := strsplit(col1, ",", fixed=TRUE)]
x[, col4 := col3[[1]][col2], by=ind]

#                           col1 col2 ind                        col3   col4
# 1:           hello,how,are,you    1   1           hello,how,are,you  hello
# 2:           hello,how,are,you    2   2           hello,how,are,you    how
# 3:           hello,how,are,you    3   3           hello,how,are,you    are
# 4: Lirum, Larum, Löffel, Stiel    4   4 Lirum, Larum, Löffel, Stiel  Stiel

OTHER TIPS

Here's another option, that combines the result of strsplit into a matrix and then uses matrix subsetting:

x <- data.table(col1=rep("hello,how,are,you",4),col2=1:4)

x[, col3 := do.call(rbind, strsplit(col1, split = ",", fixed = TRUE))
               [matrix(c(1:.N, col2), ncol = 2)]]
x
#                col1 col2  col3
#1: hello,how,are,you    1 hello
#2: hello,how,are,you    2   how
#3: hello,how,are,you    3   are
#4: hello,how,are,you    4   you

I hope following can work for you...

Solution 1 : if col1 don't have fixed word length

col1 = c(paste(c('hello','how','are','you'),1,sep='-',collapse=','),
         paste(c('hello','how','are','you'),2,sep='-',collapse=','),
         paste(c('hello','how','are','you'),3,sep='-',collapse=','),
         paste(c('hello','how','are','you'),4,sep='-',collapse=','))

x<-data.table(col1=col1,col2=1:4)
x$col3 = NA
for(i in 1:nrow(x)){
  x$col3[i] = strsplit(x$col1[i],',')[[1]][x$col2[i]]
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top