sapply

lapply / purrr::map like function that allows access to the index by default?

帅比萌擦擦* 提交于 2021-02-20 04:29:08
问题 There's a workaround to allow access the index inside a s/lapply e.g. x <- list(a=11,b=12,c=13) lapply(seq_along(x), function(y, n, i) { paste(n[[i]], y[[i]]) }, y=x, n=names(x)) Is there any function like s/lapply (or like purrr::map() ) which allows access to the index in the simplest way possible, which I guess would be to simply supply its desired name to the initial function call and nothing more; map_with_index <- function(.x, .f, index) { # Same as purrr::map() # ..but whatever string

Alternative for sample

Deadly 提交于 2021-02-10 05:29:05
问题 I have the following sample code that uses sapply which takes long to process (since executed many times): samples = sapply(rowIndices, function(idx){ sample(vectorToDrawFrom, 1, TRUE, weights[idx, ]) }) The issue is that I have to draw from the weights which are in the matrix, dependent on the indices in rowIndices . Does somebody have a better idea in mind to draw from the rows of the matrix? Reproducable example: rowIndices = floor(runif(1000, 1, 100)) vectorToDrawFrom = runif(5000, 0.0, 2

Passing list element names as a variable to functions within lapply

放肆的年华 提交于 2021-02-08 03:45:29
问题 I have a named list of data and a custom function I want to apply to the data: #Some example data d.list <- list(a = c(1,2,3), b = c(4,5,6), c = c(7,8,9)) #A simple function to process some data, write an output graph, and return an output myfun <- function(data, f.name) { y <- list() y[1] <- data[1] + data[2] + data[3] y[2] <- data[3] - data[1]/ data[2] y[3] <- data[2] * data[3] - data[1] svg(filename = f.name, width = 7, height = 5, pointsize = 12) plot.new() plot(data, y) dev.off() return

R substr function on multiple columns

懵懂的女人 提交于 2021-02-05 11:57:23
问题 I have 3 columns. First column has unique ID, second and third columns have string data and some NA data. I need to extract info from column 2 and put it in separate columns and do the same thing for column 3. I am building a function as follows, using for loops. I need to split the columns after the third letter. [For example in the V1 column below, I need to break AAAbbb as AAA and bbb and put them in separate columns. I know I can use substr to do this. I am new to R, please help. UID * V1

R substr function on multiple columns

こ雲淡風輕ζ 提交于 2021-02-05 11:57:21
问题 I have 3 columns. First column has unique ID, second and third columns have string data and some NA data. I need to extract info from column 2 and put it in separate columns and do the same thing for column 3. I am building a function as follows, using for loops. I need to split the columns after the third letter. [For example in the V1 column below, I need to break AAAbbb as AAA and bbb and put them in separate columns. I know I can use substr to do this. I am new to R, please help. UID * V1

apply-strsplit-rowwise including sort and nested paste

£可爱£侵袭症+ 提交于 2021-01-28 05:36:01
问题 I guess I just don't see it, but all the similar thing I found on the Net, in the Mailinglist archives or the FAQ could not really elucidate my issue. The closest I have found was this: apply strsplit rowwise I have a df, with two character columns and one numerical column. Filled like this: df=data.frame(name1=c("A","B","C","D"), name2=c("B","A","D","C"), nums=c(1,1,4,4), stringsAsFactors=F) Now I would like to find the unique rows in this, however, only based on the two name columns. And

Applying multiple function via sapply

一个人想着一个人 提交于 2021-01-28 03:55:24
问题 I'm trying to replicate solution on applying multiple functions in sapply posted on R-Bloggers but I can't get it to work in the desired manner. I'm working with a simple data set, similar to the one generated below: require(datasets) crs_mat <- cor(mtcars) # Triangle function get_upper_tri <- function(cormat){ cormat[lower.tri(cormat)] <- NA return(cormat) } require(reshape2) crs_mat <- melt(get_upper_tri(crs_mat)) I would like to replace some text values across columns Var1 and Var2 . The

Performing a linear model in R of a single response with a single predictor from a large dataframe and repeat for each column

99封情书 提交于 2020-12-15 01:47:19
问题 It might not be very clear from the title but what I wish to do is: I have a dataframe df with, say, 200 columns and the first 80 columns are response variables (y1, y2, y3, ...) and the rest of 120 are predictors (x1, x2, x3, ...). I wish to compute a linear model for each pair – lm(yi ~ xi, data = df) . Many problems and solutions I have looked through online have a either a fixed response vs many predictors or the other way around, using lapply() and its related functions. Could anyone who

Performing a linear model in R of a single response with a single predictor from a large dataframe and repeat for each column

血红的双手。 提交于 2020-12-15 01:44:08
问题 It might not be very clear from the title but what I wish to do is: I have a dataframe df with, say, 200 columns and the first 80 columns are response variables (y1, y2, y3, ...) and the rest of 120 are predictors (x1, x2, x3, ...). I wish to compute a linear model for each pair – lm(yi ~ xi, data = df) . Many problems and solutions I have looked through online have a either a fixed response vs many predictors or the other way around, using lapply() and its related functions. Could anyone who