# What this line of code does using names and %in%?

I was wondering what the code below does. data=mydata[,names(mydata) %in% variables\$variable] >Solution : It subsets the columns of ‘mydata’ that are also found in the variable column in ‘variables’. It can be also written as mydata[intersect(names(mydata), variables\$variable)] Or with dplyr library(dplyr) mydata %>% select(any_of(variables\$variable))

# Creating unique object names for list entries

I have example data as follows: listoflists= list( list(a = c(1,’a’,3,4) , b = c(5,’b’,6,7) ), list(a = c(1,’a’,2,6) , b = c(5,’b’,0,8) ), list(d = c(1,’a’,2,6) , b = c(5,’b’,0,8) ), list(d = c(1,’a’,2,3) , b = c(5,’b’,0,8) , a = c(5,’b’,0,8)), list(d = c(1,’a’,1,1)) ) I would like rename the names (such as… Read More Creating unique object names for list entries

# Extract names of second [given] level of nested list in R

I have a nested list. Each level of this list is named (as in provided dummy example). I want to extract the names (unique) from the 2nd level of my initial list, so I will be able to use them in some further operations. I know how to do it in two steps, but I… Read More Extract names of second [given] level of nested list in R

# Get vector of column names depending on logicals of same dataframe

I have a named dataframe containig logicals with missings and I want to get a vector with the column names where values are TRUE (going down the rows and, if multiple TRUEs in one row, going from left to right). Here an example: df <- data.frame(a= c(FALSE, NA, TRUE, TRUE), b= c(TRUE, FALSE, FALSE, NA),… Read More Get vector of column names depending on logicals of same dataframe