similar to: help with simple function

Displaying 20 results from an estimated 4000 matches similar to: "help with simple function"

2008 Jun 02
4
NOT-SO-SIMPLE function!
I am trying to set up a function which processes my data according to the following rules: 1. if (x[i]==0) NA 2. if (x[i]>0) log(x[i]/(number of consecutive zeros immediately preceding it +1)) The data this will apply to include a variety of whole numbers not limited to 1 & 0, a number of which may appear consecutively and not separated by zeros. Below is an example with a detailed
2009 Nov 15
2
Relase positive with log and zero of negative with 0
This is a very simple question but I couldn't form a site search quesry that would return a reasonable result set. Say I have a vector: x <- c(0,2,3,4,5,-1,-2) I want to replace all of the values in 'x' with the log of x. Naturally this runs into problems since some of the values are negative or zero. So how can I replace all of the positive elements of x with the log(x) and the
2008 Oct 03
1
NA's in segmented
I am trying to fit a very simple broken stick model using the package "segmented" but I have hit a roadblock. > str(data) 'data.frame': 18 obs. of 2 variables: $ Bin : num 0.25 0.75 1.25 1.75 2.25 2.75 3.25 3.75 4.25 4.75 ... $ LnFREQ: num 5.06 4.23 3.50 3.47 2.83 ... I fit the lm easily: > fit.lm<-lm(LnFREQ~Bin, data=id07) But I keep getting an error
2009 Jul 30
3
Looping through R objects with $ operator and tags
Hi all, Suppose I want to set the values in a column to the log of the values of another column like so: object$LogDist <- log10(object$Distance) How do I loop through the objects if I have object1, object2, etc to perform this function? object1$LogDist <- log10(object1$Distance) object2$LogDist <- log10(object2$Distance) object3$LogDist <- log10(object3$Distance) I was trying to
2017 Jun 08
3
Math ops behaviour with multiple classes
I would expect that several math operations should always return values with a class of numeric. If the input is defined with multiple classes, however, the class attribute is preserved. I would think this may have some unintended side-effects. Here's an example: > sessionInfo()$R.version$version.string [1] "R version 3.4.0 (2017-04-21)" > x <- seq.int(5) >
2017 Jun 08
0
Math ops behaviour with multiple classes
I think you may be confusing (S3) class and ?mode. > x <- seq.int(1:3) > class(x) [1] "integer" > mode(x) [1] "numeric" > class(x+.5) ## coercion [1] "numeric" > mode(x+.5) [1] "numeric" But note: > y <- as.integer(1) > class(y) [1] "integer" > class(y) <- "foo" > mode(y) [1] "numeric" >
2017 Jun 08
1
Math ops behaviour with multiple classes
Thanks Bert, I think we agree on the current behaviour, but I'm still not sure if it's desirable. The mode isn't used for method dispatch. In the following example, I have to write `log.foo` in order for the correct method to be called. > x <- seq.int(5) > class(x) <- c("integer", "foo") > half <- function(x) UseMethod("half") >
2010 Dec 15
1
lmList and lapply(... lm) different std. errors
Am I trying to perform multiple linear regressions on each 'VARIABLE2'. I figured out that there are different ways, using the following code: (data is given at the end of this message) reg <- lapply(split(TRY, VARIABLE2), function(X){lm(X2 ~ X3, data=X)}) lapply(reg, summary) Which produces the following: $`1` Call: lm(formula = X2 ~ X3, data = X) Residuals: Min
2007 Jun 19
4
How do I avoid a loop?
Hi, I start with an array of booleans: x <- c( TRUE, TRUE, TRUE, FALSE, FALSE, TRUE, TRUE, FALSE, TRUE ); I want to define an y <- f(x) such that: y <- c( 1, 2, 3, 0, 0, 1, 2, 0, 1 ); In other words, do a cumsum when I see a TRUE, but reset to 0 if I see a FALSE. I know I can do this with a very slow and ugly loop or maybe use apply, but I was hoping there are some R experts out
2008 Jan 21
2
experiments with slot functions and possible problems NOTE
Hello, first of all, thanks to LT for \pkg{codeutils}. I agree that it is indeed very useful to identify errors and also to encourage re-thinking past solutions. My problem: I want to compare different sets of related sub-functions which should be used alternatively by the same top-level function. Sets of related functions should be bound together (as lists) and the workspace should be as clean
2009 Oct 16
1
Converting dataframe to matrix
Hi, I'm experimenting with a few learners that require a matrix as their input. (Currently svmpath, vbmp, etc.) I currently have a dataframe with 50 columns and 20,000 rows. I tried using: x <- as.matrix(my_data.frame) If I then as, "is.matrix(x)", I get TRUE. However everywhere I've tried to use the matrix returns errors. Is there a step I'm missing? Thanks! -N
2013 Mar 22
3
Distance calculation
Hi Elisa, I hope this is what you wanted. dat1<-read.csv("peaks.csv",sep=",") #Subset dat2<-dat1[1:5,] res1<-do.call(cbind,lapply(seq_len(nrow(dat2)),function(i) do.call(rbind,lapply(split(rbind(dat2[i,],dat2[-i,]),1:nrow(rbind(dat2[i,],dat2[-i,]))), function(x) {x1<-rbind(dat2[i,],x);
2006 Sep 17
2
histogram frequency weighing
Fellow R-helpers, Suppose we create a histogram as follows (although it could be any vector with zeroes in it): R> lenh <- hist(iris$Sepal.Length, br=seq(4, 8, 0.05)) R> lenh$counts [1] 0 0 0 0 0 1 0 3 0 1 0 4 0 2 0 5 0 6 0 10 0 9 0 4 0 [26] 1 0 6 0 7 0 6 0 8 0 7 0 3 0 6 0 6 0 4 0 9 0 7 0 5 [51] 0 2 0 8 0 3 0 4 0 1 0 1 0 3
2013 Feb 13
5
spearman correlation and p-value as a matrix
I have two data matrices that I want to make the correlation between each column from data1 and each column from data 2 and also calculate the p-value Matrices dont have the same size and I tried such a script. > bg <- read.table (file.choose(), header=T, row.names) > bg > Otu00022 Otu00029 Otu00039 Otu00042 Otu00101 Otu00105 Otu00125 Otu00131 Otu00137 Otu00155 Otu00158 Otu00172
2012 Feb 05
1
how to REPLACE VALUES in a dataframe
Hi, I have two data frames (u and v). > u   coe      nam 1   0     Time 2   0    Poten 3   0   AdvExp 4   0    Share 5   0   Change 6   0 Accounts 7   0     Work 8   0   Rating > v       coeff    enter 1 0.7272727 Accounts 2 0.3211112     Time 3 0.0500123    Poten I want to update the values of coe in u by using the values ofcoeff in v. That is, I want to get the following result >
2015 Apr 23
3
model frames and update()
This issue has arisen within my anova.coxph routine, but is as easily illustrated with glm. testdata <- data.frame(y= 1:5, n= c(8,10,6,20,14), sex = c(0,1,0,1,1), age = c(30,20,35,25,40)) fit <- glm(cbind(y,n) ~ age + sex, binomial, data=testdata, model=TRUE) saveit <- fit$model update(fit, .~. - age, data=saveit)
2009 Mar 28
1
Find inflection points using smooth.spline
Is there any way to identify or infer the inflection points in a smooth spline object? I am doing a comparison of various methods of time-series analysis (polynomial regression, spline smoothing, recursive partitioning) and I am specifically interested in obtaining the julian dates associated with the inflection points inferred by the various models. Tyler e.g.
2012 Jun 19
2
how to use by function
hi all, Assume I have data like data<-rbind(c(1,2),c(1,3),c(2,1),c(3,2),c(3,4)) I want to get some matrix like 1,2,3 2,NA,NA 3,2,4 I'm using by mat<-matrix(NA,3,3) by(data,data[,1],mat[data[,1],]<-c(data[,2])) but it doesn't work. Any ideas? thanks, cowboy
2005 Apr 29
2
Subarrays
Define an array > v<-1:256 > dim(v)<-rep(4,4) Subarrays can be obtained as follows: > v[3,2,,2] [1] 71 87 103 119 > v[3,,,2] [,1] [,2] [,3] [,4] [1,] 67 83 99 115 [2,] 71 87 103 119 [3,] 75 91 107 123 [4,] 79 95 111 127 In the general case this procedure is very tedious. Given an array A, dim(A)=(dim_1,dim_2,...,dim_d) and two vectors
2005 Jun 20
1
(no subject)
R friends, I am using R 2.1.0 in a Win XP . I have a problem working with lists, probably I do not understand how to use them. Lets suppose that a set of patients visit a clinic once a year for 4 years on each visit a test, say 'eib' is performed with results 0 or 1 The patients do not all visit the clinic the 4 times but they missed a lot of visits. The test is considered positive if it