lapply

Replace first occurrence of “:” but not second in R

岁酱吖の 提交于 2019-11-28 13:25:48
In order to be able to process I'd like to replace the first occurrence of a : in a string (which is my marker, that a speech begins). text <- c("Mr. Mark Francois (Rayleigh) (Con): If the scheme was so poorly targeted, why were the Government about to roll it out to employees in the Department of Trade and Industry and the Department for Work and Pensions on the very day the Treasury scrapped it? The CBI and the TUC have endorsed the scheme, which has helped 500,000 people and their families to improve their computer skills. When the Chancellor announced the original concession, he told the

How to set ggplot x-label equal to variable name during lapply?

耗尽温柔 提交于 2019-11-28 12:43:11
I'm making plots of one y variable against multiple x variables. I have a working solution using lapply. However, I can't manage to write the name of the x variable as the x label for each plot. Here's a simplified example of what I have: The goal is to plot the y variable against each x variable resulting in three plots and adding the name of each x variable as the x axis label. Generate a dataframe with one y variable and three x variables: df <- data.frame(y.variable=c(11:20), x1=c(21:30),x2=c(1:10),x3=c(31:40)) A function that is supposed to retrieve the variable name as a string: get_name

Rename Columns of Data.frame in list

半世苍凉 提交于 2019-11-28 10:23:36
I am trying to use lapply (and want the solution with lapply) to rename columns of a data.frame located in a list, but it's returning names, not the renamed data.frames: # define list li <- list(u_n = data.frame(x = 1:3), r_l = data.frame(y = 4:6)) # trying to rename columns after the element of the list they're located in li_2 <- lapply(1:length(li), function(x,y) colnames(y[[x]]) <- names(y)[x], y = li) However, this returns: [[1]] [1] "u_n" [[2]] [1] "r_l" If I use the same method as the function specified in lapply individually, it does work: li[1] $u_n x 1 1 2 2 3 3 colnames(li[[1]]) <-

Fast alternative to split in R

◇◆丶佛笑我妖孽 提交于 2019-11-28 07:15:52
问题 I'm partitioning a data frame with split() in order to use parLapply() to call a function on each partition in parallel. The data frame has 1.3 million rows and 20 cols. I'm splitting/partitioning by two columns, both character type. Looks like there are ~47K unique IDs and ~12K unique codes, but not every pairing of ID and code are matched. The resulting number of partitions is ~250K. Here is the split() line: system.time(pop_part <- split(pop, list(pop$ID, pop$code))) The partitions will

Using lapply to change column names of a list of data frames

梦想与她 提交于 2019-11-28 07:01:19
I'm trying to use lapply on a list of data frames; but failing at passing the parameters correctly (I think). List of data frames: df1 <- data.frame(A = 1:10, B= 11:20) df2 <- data.frame(A = 21:30, B = 31:40) listDF <- list(df1, df2,df3) #multiple data frames w. way less columns than the length of vector todos Vector with columns names: todos <-c('col1','col2', ......'colN') I'd like to change the column names using lapply: lapply (listDF, function(x) { colnames(x)[2:length(x)] <-todos[1:length(x)-1] } ) but this doesn't change the names at all. Am I not passing the data frames themselves, but

Same function over multiple data frames in R

前提是你 提交于 2019-11-28 06:35:42
I am new to R, and this is a very simple question. I've found a lot of similar things to what I want but not exactly it. Basically I have multiple data frames and I simply want to run the same function across all of them. A for-loop could work but I'm not sure how to set it up properly to call data frames. It also seems most prefer the lapply approach with R. I've played with the get function as well to no avail. I apologize if this is a duplicated question. Any help would be greatly appreciated! Here's my over simplified example: 2 data frames: df1, df2 df1 start stop ID 0 10 x 10 20 y 20 30

Using lapply on a list of models

一个人想着一个人 提交于 2019-11-28 05:29:25
问题 I have generated a list of models, and would like to create a summary table. As and example, here are two models: x <- seq(1:10) y <- sin(x)^2 model1 <- lm(y ~ x) model2 <- lm(y ~ x + I(x^2) + I(x^3)) and two formulas, the first generating the equation from components of formula get.model.equation <- function(x) { x <- as.character((x$call)$formula) x <- paste(x[2],x[1],x[3]) } and the second generating the name of model as a string get.model.name <- function(x) { x <- deparse(substitute(x))

Using lapply to apply a function over list of data frames and saving output to files with different names

余生长醉 提交于 2019-11-28 04:59:08
I have a list of data frames and have given each element in the list (e.g. each data frame) a name: e.g. df1 <- data.frame(x = c(1:5), y = c(11:15)) df2 <- data.frame(x = c(1:5), y = c(11:15)) mylist <- list(A = df1, B = df2) I have a function that I want to apply to each data frame; In this function, I want to include a line to write the results to file (eventually I want to do more complicated things like save plots of the correlation between two variables for each data frame but thought I'd start simple) e.g. NewVar <- function(mydata, whichVar, i) { mydata$newVar <- mydata[, whichVar] + 1

combination of expand.grid and mapply?

孤街浪徒 提交于 2019-11-28 03:32:11
问题 I am trying to come up with a variant of mapply (call it xapply for now) that combines the functionality (sort of) of expand.grid and mapply . That is, for a function FUN and a list of arguments L1 , L2 , L3 , ... of unknown length , it should produce a list of length n1*n2*n3 (where ni is the length of list i ) which is the result of applying FUN to all combinations of the elements of the list. If expand.grid worked to generate lists of lists rather than data frames, one might be able to use

Error in heatmap.2 (gplots)

天涯浪子 提交于 2019-11-28 01:15:42
问题 Ive moved on to a new server and Installed R version 3.0 on it. (gplots library was no longer available for 2.14) Using a script that worked for version 2.14 I now encounter a problem generating a heatmap. In R version 3 I get an error: Error in lapply(args, is.character) : node stack overflow Error in dev.flush() : node stack overflow Error in par(op) : node stack overflow In R version 2.14 I get an error: Error: evaluation nested too deeply: infinite recursion / options(expressions=)? Which