apply

Apply List of functions on List of columns based on different combinations

萝らか妹 提交于 2019-12-06 19:49:40
I have a dataframe df with three categorical variables cat1 , cat2 , cat3 and two continuous variables con1 , con2 . I would like to compute list of functions sd , mean on list of columns con1 , con2 based on different combinations of list of columns cat1 , cat2 , cat3 . I have done them explicitly subsetting all different combinations. # Random generation of values for categorical data set.seed(33) df <- data.frame(cat1 = sample( LETTERS[1:2], 100, replace=TRUE ), cat2 = sample( LETTERS[3:5], 100, replace=TRUE ), cat3 = sample( LETTERS[2:4], 100, replace=TRUE ), con1 = runif(100,0,100), con2

Compare every 2 rows and show mismatches in R

房东的猫 提交于 2019-12-06 15:20:22
问题 I have searched a lot and tried on my own too, but couldn't find solution for this particular problem. For every 2 rows ('key' is common), I have to find mismatches in every column and highlight them in an organized way like below. The output should be in the following format: COLUMN_NAME is not matching for records below: PRINT COMPLETE RECORDS ... COLUMN_NAME is not matching for records below: PRINT COMPLETE RECORDS ... COLUMN_NAME is not matching for records below: PRINT COMPLETE RECORDS .

How to apply patches on the top of a git tree preventing duplication?

我的梦境 提交于 2019-12-06 13:45:18
I'm seeking advice for a problem that I thought to be simple, and it might be simple indeed by creating a small script, but I think there should already be a way to do that with git/quilt/stgit. I'm not exactly good at git and this is causing some issues to me. My problem: I've got a git tree (linux kernel) and a number of patches. What happens, such patches were intended for and older version of the kernel, and many of them have already been applied to my tree. The patches start with an header line like From b1af4315d823a2b6659c5b14bc17f7bc61878ef4 (timestamp) and by doing something like git

constructing a function using colnames as variables

被刻印的时光 ゝ 提交于 2019-12-06 12:22:06
I'd like to collect terms under multiple columns of the annot data.frame . Below is the first row of information for a toy datset for annot. colnames(annot) # [1] "HUGO.Name" "Common.Name" "Gene.Class" "Cell.Type" "Annotation" annot[1,] # HUGO.Name Common.Name Gene.Class Cell.Type # 1 CCL1 CCL1 Immune Response - Cell Type specific aDC # Annotation # 1 Cell Type specific, Chemokines and receptors, Inflammatory response So far, I've been writing the colnames iteratively, but I'd like to learn how to write a function to loop through all columns of annot (and more generally other data.frames ).

Conditional subsetting by POSIXct interval and another field containing interval

浪尽此生 提交于 2019-12-06 11:12:00
Given a dataset Dat where I have species (SP), Area (AR), and Time (TM) (in POSIXct). I want to subset the data for individuals that were present with Species A, within a half hour prior and after it was recorded, and within the same area, including two adjacent areas (+ and - 1). For example, if species A was present at 1:00 on area 4, I wish to subset all species present from 12:30 to 1:30 in the same day in areas 3,4 and 5. As an example: SP TM AR B 1-jan-03 07:22 1 F 1-jan-03 09:22 4 A 1-jan-03 09:22 1 C 1-jan-03 08:17 3 D 1-jan-03 09:20 1 E 1-jan-03 06:55 4 D 1-jan-03 09:03 1 E 1-jan-03

Using lapply and which to subset dataframe by both characteristic and fuction

一笑奈何 提交于 2019-12-06 10:31:20
I have a dataframe with 5 dimensions of data that looks like this: > dim(alldata) [1] 162 6 > head(alldata) value layer Kmultiplier Resolution Season Variable 1: 0.01308008 b .01K 1km Baseflow Evapotranspiration 2: 0.03974779 b .01K 1km Peak Flow Evapotranspiration 3: 0.02396524 b .01K 1km Summer Flow Evapotranspiration 4: -0.15670996 b .01K 1km Baseflow Discharge 5: 0.06774948 b .01K 1km Peak Flow Discharge 6: -0.04138313 b .01K 1km Summer Flow Discharge What I'd like to do is get the mean of the value column for certain 'characteristics' of the data based on the other columns. So I use which

R: Why am I not getting type or class “factor” after converting columns to factor?

妖精的绣舞 提交于 2019-12-06 10:11:56
I have the following setup. df <- data.frame(aa = rnorm(1000), bb = rnorm(1000)) apply(df, 2, typeof) # aa bb #"double" "double" apply(df, 2, class) # aa bb #"numeric" "numeric" Then I try to convert one of the columns to "factor". But as you can see below, I am not getting any "factor" type or classes. Am I doing anything wrong ? df[, 1] <- as.factor(df[, 1]) apply(df, 2, typeof) # aa bb #"character" "character" apply(df, 2, class) # aa bb #"character" "character" Sorry I felt my original answer badly written. Why did I put that "matrix of factors" in the very beginning? Here is a better try.

Can you implement 'sweep' using apply in R?

人走茶凉 提交于 2019-12-06 07:30:36
I'm brushing up on my R skills and finally feel like I've mastered the strange sweep function e.g. df <- data.frame(a = 1:3, b = 2:4) sweep(df, MARGIN = 2, STATS = c(5, 10), FUN = "*") ## a b ## 1 5 20 ## 2 10 30 ## 3 15 40 and more usefully here , on a tutorial I'm working on implementing a spatial interaction model in R. They say that a sign you understand something is that you can say it in many ways, and I think this applies more in programming than almost anywhere else. Yet, despite the problem that sweep solves seeming apply -esque, I have NO IDEA whether they are to some degree

addEventListener using apply()

这一生的挚爱 提交于 2019-12-06 07:12:13
I'm trying to invoke addEventListener() using apply() method. The code is like: function rewrite(old){ return function(){ console.log( 'add something to ' + old.name ); old.apply(this, arguments); } } addEventListener=rewrite(addEventListener); It doesn't work. The code works for normal JavaScript method, for example, function hello_1(){ console.log("hello world 1!"); } hello_1=rewrite(hello_1); Need help! Thanks! You can't count on addEventListener being a real Javascript function, unfortunately. (This is true of several other host-provided functions, like window.alert ). Many browsers do the

Rolling percentage add along column

懵懂的女人 提交于 2019-12-05 23:59:55
I feel this should be easy in base R but I just can't figure it out. I have a simple dataframe, let's say it looks like this tbl <- read.table(text = "Field1 Field2 100 200 150 180 200 160 280 250 300 300 300 250", header = TRUE) Now, what I want to do is create a function that will apply a rolling % addition, something like: fn <- function(tbl, pct) {} which accepts the dataframe above as tbl . It adds a percentage fraction of the current row to the NEXT row down based on pct , and rolls this almost in a cumulative fashion. For example, fn(tbl$Field1, 0.1) would generate the following results