On Tue, 2005-08-23 at 10:12 -0500, Greg Blevins wrote:
Hello,
I have struggled, for longer than I care to admit, with this seemingly
simple problem, but I cannot find a solution other than the use of
long drawn out ifelse statements. I know there has to be a better
way. Here is stripped
x - data.frame(q33a=3:4,q33b=5:6,q35a=1:2,q35b=2:1)
y - list()
for (i in grep(q33, colnames(x), value=TRUE))
+y[[sub(q33,,i)]] - ifelse(x[[sub(q33,q35,i)]]==1, x[[i]], NA)
as.data.frame(y)
a b
1 3 NA
2 NA 6
# if you really want to create new variables rather
# than have them
You can do the following without resorting to a hard coded loop
sapply( paste(q35, letters[1:grep(r, letters)], sep=), function(x)
ifelse(temp[, x]%in%1,temp[, sub(5, 3, x)],NA)
as the following example shows
temp - matrix(sample(c(0,1), 360, replace=T), nrow=10)
colnames(temp) - c(paste(q33,
Does this do what you want?
foo.df - data.frame(x = rnorm(12), y = runif(12), z = factor(rep(1:3,4)))
bar.mat - matrix(NA, nrow = ncol(foo.df)-1, ncol = nlevels(foo.df$z))
for(i in 1:(ncol(foo.df)-1))
{
bar.mat[i,] - xtabs(foo.df[,i] ~ foo.df$z)
}
bar.mat
There's probably a slicker way with
I'm quite sure there're better ways, but this works for me:
dat - data.frame(y=rnorm(30), x1=runif(30), x2=runif(30), x3=runif(30),
+ group=factor(rep(1:3, each=10)))
getCoef - function(dat) {
+ apply(dat[,c(x1,x2,x3)], 2,
+ function(x) lm.fit(cbind(1, x),
Here's one simplistic solution, perhaps there are better ones:
# Make some test data and place in dataframe
x1=rnorm(20)
x2=rnorm(20)
x3=rnorm(20)
x4=as.factor(sample(c(G1,G2,G3),20,replace=T))
y1=2*x1+4*x2+0.5*x3+as.numeric(x4)+rnorm(20)
df=data.frame(y1,x1,x2,x3,x4)
# Now create the ouput
Note that there is a QUESTION at the end regarding
random effects.
Suppose your data frame is df and has components
y, x1, x2, x3 and u where u is a factor.
1. There was a problem posted about doing repeated regressions
(search for Operating on windows of data) last month that
has
On Sat, 3 Apr 2004, Gabor Grothendieck wrote:
2. Another possibility is to create a giant regression that does
all the usergroup specific regressions at once and then repeat
it without the usergroup variable to get the rest.
df2 is a new data frame that strings out all the x variables into
On Wed, 27 Aug 2003, Simon Fear wrote:
I tried to define a function like:
fnx - function(x, by.vars=Month)
print(by(x, by.vars, summary))
But this doesn't work (does not find x$Month; unlike other functions,
such as
subset(), the INDICES argument to by does not look for variables in
Simon Fear [EMAIL PROTECTED] writes:
I tried to define a function like:
fnx - function(x, by.vars=Month)
print(by(x, by.vars, summary))
But this doesn't work (does not find x$Month; unlike other functions,
such as
subset(), the INDICES argument to by does not look for variables in
August 2003 14:08
To: Simon Fear
Cc: [EMAIL PROTECTED]
Subject: Re: [R] seeking help with with()
Security Warning:
If you are not sure an attachment is safe to open please contact
Andy on x234. There are 0 attachments with this message
11 matches
Mail list logo