Questions tagged [lapply]

lapply is a function in R that returns a list of the same length as given argument X, each element of which is the result of applying given function to the corresponding element of X

3969 questions
1
vote
1 answer

How to subset matrices in a list based on another list with values indicating column numbers

I have a list of matrices (mat_list). I want to create a new list with a selected subset of columns from each matrix. I have another list of numerics (col_list) which indicates column numbers to keep. Example dataset: > mat_list <-…
Neuroguy
  • 131
  • 1
  • 1
  • 9
1
vote
0 answers

Get list element name within lapply()

I have a list of dataframes. I'm trying to use lapply to produce a plot for each dataframe, with the dataframe's name as the plot title. For example, this works, without the plot title: # example…
Ant Dancer
  • 11
  • 3
1
vote
2 answers

Modify dataframes in list with R

I want to work on columns and row-names of a bunch of data frames, say W1, W2, W3, whose names are listed in a previously built list, say W. I've seen quite a few similar questions, but none seem to address my difficulty: > W <- list(W1, W2, W3) > …
Cbhihe
  • 511
  • 9
  • 24
1
vote
1 answer

Using is.na with Sapply function in R

Can anyone tell me what the line of code written below do? sapply(X, function(x) sum(is.na(x))) / nrow(airports) * 100 What is understood is that it will drop NAs when it applies the sum function but keeps them in the matrix. Any help is…
srkale
  • 21
  • 1
  • 3
1
vote
1 answer

Using lapply to Sample dataframe, include new column and reorder columns

df<-structure(list(BBAS3 = c(22.85, 22.78, 22.8, 22.22, 22.51, 21.11, 20.84, 20.79, 20.67, 20.9, 20.95, 20.7, 21.03, 21.96, 21.9, 21.8, 21.9, 22.49, 22.65, 22.9, 22.19, 22.44, 21.66, 22.5, 22.96, 23.36, 23.64, 23.46, 23.85, 23.74, 23.9, 23.97,…
Laura
  • 675
  • 10
  • 32
1
vote
1 answer

Calculating means from tall data or wide data in R

I'm a beginner-intermediate R user that started learning R for laboratory research a few months ago. Thanks for your patience---especially if this ends up being a really stupid simple problem. Problem The tables as a reproducible example The…
M. L.
  • 25
  • 7
1
vote
1 answer

Removing string pattern from dataframe (Twitter data in RStudio)

I have a large dataframe (~500,000 observations) consisting of Twitter data (i.e. username, rewtweet counts, text) in RStudio. I want to run a text analysis on the tweets, but I first need to remove retweet tags so they don't affect my keyword…
user72716
  • 263
  • 3
  • 22
1
vote
1 answer

Calculations across dataframes

I have two lists of dataframes, the first list of dfs hold values that extend down the column and the second list of dfs holds single values like this: dynamic_df_1 <- data.frame(x = 1:10) dynamic_df_2 <- data.frame(y = 1:10) df_list <-…
QAsena
  • 603
  • 4
  • 9
1
vote
0 answers

Extracting sub-elements from list of lists vs list of vectors using lapply

How does lapply extract sub-elements from a list? More specifically, how does lapply extract sub-elements from a list of lists versus a list of vectors? Even more specifically, suppose I have the following: my_list_of_lists <- list(list(a = 1, b =…
lowndrul
  • 3,715
  • 7
  • 36
  • 54
1
vote
1 answer

return list with elements from list of lists

I have a list of lists like the input example below “testccffilt”. I’m trying to return a list where I pick off the name from each list and the lag. For example, for the first list it would be: c(‘TimeToShip’,1) I’ve tried the lapply example…
user3476463
  • 3,967
  • 22
  • 57
  • 117
1
vote
1 answer

Skip specific columns in a for loop in r

Some sample code to get started here ST <- c("AL","AL","AK","AK") X1 <- c("2,21", "2,29", "3,49", "2.3") X2 <- c("10,333","10,333", "11,333", "11,333") df <- cbind(ST, X1, X2) I am trying to write a loop, either with a for loop or with lapply…
Aaron
  • 109
  • 5
1
vote
1 answer

How to make combination of sample and lapply reproducible?

The followings are functions for bootstrapping, but how can I make the result reproducible? I tried set.seed() but that does not work because the every time lapply calls function boot.lm.vector, the function just produced one simulated set and…
Teng Li
  • 13
  • 4
1
vote
4 answers

lappy conditional on variable value

I want to lappy two functions on a data set conditional on the value of a specific variable. first_function <- function(x) {return (x + 0)} second_function <- function(x) {return (x + 1)} df <- data.frame(Letters = c("A","B","B"), Numbers =…
MCS
  • 1,071
  • 9
  • 23
1
vote
1 answer

r - dealing with NAs when using lapply to select sublist elements by position

I have a list of vectors, some of which are NA. I need to use lapply to select the second-to-last element of each vector. The problem is that NAs have length 1, so I cannot access their second-to-last element. MyList <-…
NBK
  • 887
  • 9
  • 20
1
vote
1 answer

lapply or sapply for data.frames in List

I play around with Alternatives for the dplyr way of summarising data. I like the split and apply approach but need some help. library(Hmisc) library(data.table) summary <- function(x) { funs <- c(wtd.mean, wtd.var) sapply(funs, function(f)…
user7353167
1 2 3
99
100