Displaying 20 results from an estimated 30000 matches similar to: "Using apply to get group means"
2003 Dec 22
2
error propagation - hope it is correct subject
Dear all
Please, can you advice me how to compute an error, standard deviation or
another measure of variability of computed value.
I would like to do something like:
var(y) = some.function(var(x1),var(x2),var(x3))
for level F1 (2,3,...)
Let say I have some variables - x1, x2, x3 (two computed for levels of factor F
and one which is same for all levels) and I want to compute
y =
2010 Jan 08
4
Arguments of a function
Dear all,
I have a question about how to set arguments in my own function. For
example, I have a function that looks like this:
my.f <- function(a = x1, b = x2)
{
x1 = equation 1
x2 = equation 2
x3 = equation 3
y = a + b
}
x1, x2, and x3 are temporary variables (intermediate results) calculated
from other variables within the funciton. I want to use two of these three
2006 Mar 30
2
Plotting a segmented function
This might be a trivial question, but I would appreciate if anybody
could suggest an elegant way of plotting a function such as the
following (a simple distribution function):
F(x) = 0 if x<=0
=(x^2)/2 if 0<x<=1
=2x-((x^2)/2)-1 if 1<x<=2
=1 if x>2
This is just an example. In this case it is a continuous function. But
how to do it in general in an elegant way.
2004 Nov 08
1
plotting lm coeficients with their means
I am trying to write a function that will run a linear
model and plot the regression coeficients with their
corresponding means. I am having two problems. I can
get the plot with the function below, but I am having
trouble labeling the points.
function(y,x1,x2,x3,x4){
outlm<-lm(y~x1+x2+x3+x4)
imp<-as.data.frame(outlm$coef[-1])
meanvec<-c(mean(x1),mean(x2),mean(x3),mean(x4))
2008 Mar 31
1
APPLY as alternate to FOR loop?
Dear list,
Below I've written a clunky for loop that counts NA's in a row, replacing all with NA if there are
more than 3 missing values, or keeping the values if 4 or more are present. This is sample code from a very large
dataframe I'm trying to clean up.
I know there are many simpler more elegant solutions to this little problem.
Would someone be willing to show me how to
2010 Jun 22
1
applying ifelse to dataframe
The following dataframe will illustrate the problem
DF<-data.frame(name=rep(1:5,each=2),x1=rep("A",10),x2=seq(10,19,by=1),x3=rep(NA,10),x4=seq(20,29,by=1))
DF$x3[5]<-50
# we have a data frame. we are interested in the columns x2,x3,x4 which
contain sparse
# values and many NA.
DF
name x1 x2 x3 x4
1 1 A 10 NA 20
2 1 A 11 NA 21
3 2 A 12 NA 22
4 2 A 13 NA
2012 Jul 24
3
Simple reshape problem I am completely missing
I seem to be doing something really stupid or missing something really obvious but what?
I have a simple three column data.frame that I would like to reshape to wide preferably using reshape2.
An example from http://stackoverflow.com/questions/9617348/reshape-three-column-data-frame-to-matrix looked perfect except I wanted a data frame but it seemed okay. I just changed acast to dcast and it
2009 Mar 06
1
Interpreting GLM coefficients
Hi all,
I?m fitting GLM?s and I can?t interprete the coefficients when I run a
model with interaction terms.
When I run the simpliest model there is no problem:
Model1<-glm (Fishes ~ Year + I(Year^2) + Kind.Geographic +
Kind.Fishers + Zone.2 + Hours + Fishers + Month, family =
poisson(log)) # Fishes, Year, Hours, and Fishers are numeric,
Kind.Geographic, Kind.Fishers, Zone.2 and
2009 Sep 20
2
Quadratic Constraints
HI All,
I am unable to solve a optimization Problem Please Help Me out of this to
solve. The Optimization problem is as follows :-
My objective function is linear and one of the constraint is quadratic.
Min z = 5 * X1 + 9* X2 + 7.15 *X3 + 2 * X4
subject to
X1 + X2 + X3 +X4 = 9
X1 + X4 < = 6.55
X3(X3 - 3.5) >=0
X1,X2,X3,X4 >=0
Now the problem is how to solve this kind of
2012 Jan 13
2
read.table as integer
Hello,
I have a csv file with many variables, both characters and integers.
I would like to load it on R and do some operations on integer
variables, the problem is that R loads the entire dataset considering
all variables as characters, instead I would like that R makes the
distinction between the two types, because there are too many variables
to do:
x1<-as.integer(x1)
2006 Jul 08
2
String mathematical function to R-function
hello
I make a subroutine that give-me a (mathematical)
function in string format.
I would like transform this string into function ( R
function ).
thanks for any tips.
cleber
#e.g.
fun_String = "-100*x1 + 0*x2 + 100*x3"
fun <- function(x1,x2,x3){
return(
############
evaluation( fun_String )
############
)
True String mathematical function :-( :-(
> nomes
[1]
2006 Jul 03
6
macro facility in R
R 2.2 on windows XP
I have a dataset with multiple columns. Some of the columns represent
independent variables, some represent dependent variables. I would like
to run the same analyses on a fixed set of independent variables,
changing only the dependent variable, e.g.
y1-y2=x1+x2+x3
y3-y4=x1+x2+x3
y5-y6=x1+x2+x3, etc.
I know I can write a function to perform the analyses, however in order
to
2009 Jun 08
5
if else
Hi R-helpers!
I have the following dataframe:
firm<-c(rep(1:3,4))
year<-c(rep(2001:2003,4))
X1<-rep(c(10,NA),6)
X2<-rep(c(5,NA,2),4)
data<-data.frame(firm, year,X1,X2)
data
So I want to obtain the same dataframe with a variable X3
that is:
X1, if X2=NA
X2, if X1=NA
X1+X2 if X1 and X2 are not NA
So my final data is
X3<-c(15,NA,12,5,10,2,15,NA,12,5,10,2)
2009 Nov 04
3
Constrained Optimization
Hi All,
I'm trying to do the following constrained optimization example.
Maximize x1*(1-x1) + x2*(1-x2) + x3*(1-x3)
s.t. x1 + x2 + x3 = 1
x1 >= 0 and x1 <= 1
x2 >= 0 and x2 <= 1
x3 >= 0 and x3 <= 1
which are the constraints.
I'm expecting the answer x1=x2=x3 = 1/3.
I tried the "constrOptim" function in R and I'm running into some issues.
I first start off
2017 Oct 12
2
Bug in model.matrix.default for higher-order interaction encoding when specific model terms are missing
Hi,
I recently ran into an inconsistency in the way model.matrix.default
handles factor encoding for higher level interactions with categorical
variables when the full hierarchy of effects is not present. Depending on
which lower level interactions are specified, the factor encoding changes
for a higher level interaction. Consider the following minimal reproducible
example:
--------------
>
2010 Dec 14
2
How to bind models into a list of models?
Hi R-helpers,
I have a character object called dd that has 32 elements each of which
is a model formula contained within quotation marks. Here's what it
looks like:
> dd
[1] "lm(y ~ 1,data=Cement)" "lm(y ~
X,data=Cement)" "lm(y ~ X1,data=Cement)"
[4] "lm(y ~ X2,data=Cement)" "lm(y ~
2006 Oct 27
2
all.names() and all.vars(): sorting order of functions' return vector
Dear list-subscriber,
in the process of writing a general code snippet to extract coefficients
in an expression (in the example below: 0.5 and -0.7), I stumbled over
the following peculiar (at least peculiar to me:-) ) sorting behaviour
of the function all.names():
> expr1 <- expression(x3 = 0.5 * x1 - 0.7 * x2)
> all.names(expr1)
[1] "-" "*" "x1"
2020 Jul 22
3
Invisible names problem
I ran into strange behavior when removing names.
Two ways of removing names:
i <- rep(1:4, length.out=20000)
k <- c(a=1, b=2, c=3, d=4)
x1 <- unname(k[i])
x2 <- k[i]
x2 <- unname(x2)
Are they identical?
identical(x1,x2) # TRUE
but no
identical(serialize(x1,NULL),serialize(x2,NULL)) # FALSE
But problem is with serialization type 3, cause:
2008 Mar 29
1
Tabulating Sparse Contingency Table
I have a sparse contingency table (most cells are 0):
> xtabs(~.,data[,idx:(idx+4)])
, , x3 = 1, x4 = 1, x5 = 1
x2
x1 1 2 3
1 0 0 31
2 0 0 112
3 0 0 94
, , x3 = 2, x4 = 1, x5 = 1
x2
x1 1 2 3
1 0 0 0
2 0 0 0
3 0 0 0
, , x3 = 3, x4 = 1, x5 = 1
x2
x1 1 2 3
1 0 0 0
2 0 0 0
3 0 0 0
, , x3 = 1, x4
2004 Jul 19
10
How to compare X1 = X2 = ... = Xn?
Dear All,
I have a data frame with n columns: X1, X2, ., Xn. Now I want to create a
new column: if X1 = X2 = . = Xn, the value is 1; Otherwise, the value is 0.
How to do that in a quick way instead of doing (n choose 2) comparisons?
Thank you,
Frank
[[alternative HTML version deleted]]