lapply

na.rm function doesn't work if use more then 1 group in R

好久不见. 提交于 2019-12-11 20:54:11
问题 In this post select group before certain observations separated by grouping var in R with NA control, when using one group add na.rm=T works. But new data, where three groups data=structure(list(add = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L), .Label = "x", class = "factor"), x1 = c(0L, 2L, 0L, 0L, 0L, 0L, 0L, 0L, 0L,

R data frame extracting non-sequential columns and creating new sub data frames

霸气de小男生 提交于 2019-12-11 20:24:55
问题 I have a data frame consisting of 25 columns and 30,000 rows. I want to create sub-data frames that contain all rows but only certain columns that are in non-sequential order. As an example, a sub data frame containing columns 1, 12, 15, and 25. I have been playing around with lapply but I dont know how to create the sub data frames with columns that are in non-sequential order. Thanks. 回答1: Information from data.frame s can be extracted by using the indices of a data.frame 's rows, columns,

Generate multiple lattice plots on a grid using lapply in R

≡放荡痞女 提交于 2019-12-11 19:19:06
问题 How do plot multiple lattice plots onto a single lattice plot where the plots are generated using an lapply function? The following is a demonstration of what I have tried so far using the built in mtcars dataset. require(lattice) response <- c("cyl","disp","hp","drat") par(mfrow=c(2,2)) lapply(response, function(variable) { print(xyplot(mtcars$mpg ~ mtcars[variable])) }) This produces the plots desired. However it seems to be ignoring the par(mfrow=c(2,2)) instruction and plotting each plot

R error: Wrong length for a vector, should be 2

守給你的承諾、 提交于 2019-12-11 17:51:47
问题 Here is a gist of what I want to do: I've got 2 data frames: x (id is unique) id timestamp 282462839 2012-12-05 10:55:00 282462992 2012-12-05 12:08:00 282462740 2012-12-05 12:13:00 282462999 2012-12-05 12:48:00 y (id is not unique) id value1 value2 282462839 300 100 282462839 300 200 282462839 400 300 282462999 500 400 282462999 300 150 I also have a function myfunc(id,pvalue) that computes something and returns one of the value2 values depending on pvalue and other value1s (more complicated

Combining scenario to Replace Medians by Groups in R

江枫思渺然 提交于 2019-12-11 17:08:54
问题 I have dataset mydat <- structure(list(code = structure(c(1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 2L, 2L, 2L, 2L, 2L), .Label = c("52382MCK", "52499MCK"), class = "factor"), item = c(11709L, 11709L, 11709L, 11709L, 11708L, 11708L, 11708L, 11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 11710L, 11202L,

Web Scraping multiple Links using R

无人久伴 提交于 2019-12-11 16:54:23
问题 I am working on a web scraping program to search for data from multiple sheets. The code below is an example of what I am working with. I am able to get only the first sheet on this. It will be of great help if someone can point out where I am going wrong in my syntax. jump <- seq(1, 10, by = 1) site <- paste0("https://stackoverflow.com/search?page=",jump,"&tab=Relevance&q=%5bazure%5d%20free%20tier") dflist <- lapply(site, function(i) { webpage <- read_html(i) draft_table <- html_nodes

lapply on several subsets of data frame

耗尽温柔 提交于 2019-12-11 16:42:25
问题 I have a dataframe data containing the chromosome and position of mutated nucleotides within a genome: structure(list(chrom = c(1L, 1L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 4L, 4L), pos = c(10L, 200L, 134L, 400L, 600L, 1000L, 20L, 33L, 40L, 45L, 50L, 55L, 100L, 123L)), .Names = c("chrom", "pos"), class = "data.frame", row.names = c(NA, -14L)) chrom pos 1 1 10 2 1 200 3 1 134 4 1 400 5 1 600 6 1 1000 And another tss_locations , containing the location of a feature ( tss ) within a gene and

how to use lapply instead of a for loop, to perform a calculation on a list of dataframes in R

我是研究僧i 提交于 2019-12-11 16:09:53
问题 I have a list of 30 dataframes. I want to create a vector that contains the standard deviation of the nth element of one of the columns, across all 30 data frames in the list of dataframes. I don't think I've explained that clearly. But the code for my for loop should make it clear. FFT233_sd <- list() for (i in 1:431999) {FFT233_sd[[i]] <- sd(c(FFT233_data[[1]][i,6], FFT233_data[[2]][i,6], FFT233_data[[3]][i,6], FFT233_data[[4]][i,6], FFT233_data[[5]][i,6], FFT233_data[[6]][i,6], FFT233_data

R sapply vs apply vs lapply + as.data.frame

被刻印的时光 ゝ 提交于 2019-12-11 14:15:20
问题 I'm working with some Date columns and trying to cleanse for obviously incorrect dates. I've written a function using the safe.ifelse function mentioned here. Here's my toy data set: df1 <- data.frame(id = 1:25 , month1 = seq(as.Date('2012-01-01'), as.Date('2014-01-01'), by = 'month' ) , month2 = seq(as.Date('2012-01-01'), as.Date('2014-01-01'), by = 'month' ) , month3 = seq(as.Date('2012-01-01'), as.Date('2014-01-01'), by = 'month' ) , letter1 = letters[1:25] ) This works fine for a single

lapply function with arguments I want to pick from a dataframe with a loop

前提是你 提交于 2019-12-11 13:48:12
问题 I'm still very new to R and haven't found any answer so far. Sorry to finally ask. Edition with a quick example: I want to compute a multidimensional development index based on South Africa Data. My list is composed of individual information for each year, so basically df1 is about year 1 and df2 about year2. df1<-data.frame(var1=c(1, 1,1), var2=c(0,0,1), var3=c(1,1,0)) df2<-data.frame(var1=c(1, 0,1), var2=c(1,0,1), var3=c(0,1,0)) mylist <-list (df1,df2) You can find here a very simplified