Displaying 20 results from an estimated 20000 matches similar to: "Coercing by/tapply to data.frame for more than two indices?"
2009 Jun 16
2
tapply with cbinded x
Dear List,
why does this not work?
df <- data.frame(var1 = c(3,2,1), var2 = c(6,5,4), var3 = c(9,8,7),
fac = c('A', 'A', 'B'))
tapply(cbind(df$var1, df$var2, df$var3), df$fac, mean)
Thank you,
Stefan
2008 Jul 16
2
Group level frequencies
Dear List,
I have Multi-level Data
i= Indivitual Level
g= Group Level
var1= First Variable of interest
var2= Second Variable of interest
and I want to count the frequency of "var1" and "var2" on the group
level.
I found a way, but there must be a much simpler way.
data.ml <-
data.frame(i=c(1:8),g=as.factor(c(1,1,1,2,2,3,3,3)),var1=c(3,3,3,4,4,4,4
,4),
2006 Feb 11
2
aggregate vs tapply; is there a middle ground?
Dear all,
I'm wanting to do a series of comparisons among 4 categorical variables:
a <- aggregate(y, list(var1, var2, var3, var4), sum)
This gets me a very nice 2-dimensional data frame with one column per
variable, BUT, as help for aggregate says, <<empty subsets are
removed>>. I don't see in help(aggregate) how I can change this.
In contrast,
a <- tapply(y,
2009 Feb 27
3
Making tapply code more efficient
Previously, I posed the question pasted down below to the list and
received some very helpful responses. While the code suggestions
provided in response indeed work, they seem to only work with *very*
small data sets and so I wanted to follow up and see if anyone had ideas
for better efficiency. I was quite embarrased on this as our SAS
programmers cranked out programs that did this in the blink
2009 Sep 01
2
Simple question about data.frame reduction
Hi,
this is a simple question
I have this data.frame:
> test <-
data.frame(var1=c(1,1,1,1,1,1),var2=c("a","a","b","c","d","e"),var3=c("a1","a1","b1","a1","c1","d1"))
> test
var1 var2 var3
1 1 a a1
2 1 a a1
3 1 b b1
4 1 c a1
5 1
2009 Jan 13
3
Comparing elements for equality
Suppose I have a dataframe as follows:
dat <- data.frame(id = c(1,1,2,2,2), var1 = c(10,10,20,20,25), var2 =
c('foo', 'foo', 'foo', 'foobar', 'foo'))
Now, if I were to subset by id, such as:
> subset(dat, id==1)
id var1 var2
1 1 10 foo
2 1 10 foo
I can see that the elements in var1 are exactly the same and the
elements in var2 are exactly
2010 Jan 18
1
problem of data manipulation
Hello,
See my problem below.
a<-data.frame(c("s","c","c","n","n","n"),c(rep(1,3),rep(2,3)),c(rep(2,3),rep(1,3)),c("01/01/1999","10/02/2000","13/02/2000","11/02/2000","15/02/2000","23/02/2000"))
2008 Jan 24
3
Reshaping a dataframe with conditional summary of columns - apply or reshape?
I need to do a somewhat complex rearrangement of an existing dataframe that
involves an "on-the-fly" conditional summary as well. I've tried to make
the example as simple as possible. Code to produce the object being acted
on is at the end of the message. I hope this isn't too much for a single
posting. Any guidance will be much appreciated.
The input example looks like
2009 Sep 18
2
Sum according observation
Hi listers,
I have a simple doubt...
I need to manipulate the data base as:
var1 var2
7 0.1
7 0.1
8 0.12
10 0.15
12 0.18
20 0.31
I would like to aggregate the equal cases as var1 making the sum of var2...
I would get...
var1 var2
7 0.2
8 0.12
10 0.15
12 0.18
20 0.31
Thanks in advance,
Marcio
--
View this message in context:
2009 Jun 16
1
Running stats on individual data.frames from the split() function list
Hello, and thanks in advance.
I have a data.frame from which I want to count observations that occur
on each day and determine the mean and std.error of said counts.
For instance:
x<-split(my.df, my.df$julian.days)
Although I'm still in my R learning infancy I am under the impression
that x is a list of data.frames subsetting my.df by group
(i.e.,julian.day) where day 1:366 are
2008 Apr 25
4
Equivalent of Excel pivot tables in R
Can somebody tell me how to do the equivalent of a pivot table in R ?
For example, if I have :
var1 var2 var3
a x 10
b y 20
a z 10
b z 20
a z 10
b z 20
I could have :
x y z
a 1 0 2
b 0 1 2
where entries in the table are counts of var3.
2009 Nov 29
3
How to z-standardize for subgroups?
Hi folks,
I have a dataframe df.vars with the follwing structure:
var1 var2 var3 group
Group is a factor.
Now I want to standardize the vars 1-3 (actually - there are many
more) by class, so I define
z.mean.sd <- function(data){
return.values <- (data - mean(data)) / (sd(data))
return(return.values)
}
now I can call for each var
z.var1 <- by(df.vars$var1, group,
2005 Oct 12
1
Correlation, by date, of two variables?
I have a dataset with three variables: date, var1, var2
How can I calculate the correlation, by date, between var1 and var2?
e.g.
date var1 var2
1/1/2001 5 4
1/1/2001 8 5
1/1/2001 9 7
2/1/2001 7 2
2/1/2001 2 1
2/1/2001 4 6
3/1/2001 3 5
3/1/2001 4 3
3/1/2001 6 9
3/1/2001 7 -1
the results I want:
1/1/2001
2007 May 16
1
how to reduce in a grid ?
hi dear R users,
I'm a newbie with R and excuse me if my question is stupid ... but i've read lot of documentation and I don't know how to do.
I have a dataset like
x y var1 var2
2 4 10 50
3 3 20 70
3 2 50 68
4 5 34 42
5 3 10 23
7 8 23 42
7 3 23 34
[...]
And I need to produce grids like :
y\x ]0-2] ]2-4]
2006 Feb 21
6
How to sum values across multiple variables using a wildcard?
I have a dataframe called "data" with 5 records (in rows) each of
which has been scored on each of many variables (in columns).
Five of the variables are named var1, var2, var3, var4, var5 using
headers. The other variables are named using other conventions.
I can create a new variable called var6 with the value 15 for each
record with this code:
> var6=var1+var2+var3+var4+var5
2004 Aug 17
5
Bug in colnames of data.frames?
Hi,
I am using R 1.9.1 on on i686 PC with SuSE Linux 9.0.
I have a data.frame, e.g.:
> myData <- data.frame( var1 = c( 1:4 ), var2 = c (5:8 ) )
If I add a new column by
> myData$var3 <- myData[ , "var1" ] + myData[ , "var2" ]
everything is fine, but if I omit the commas:
> myData$var4 <- myData[ "var1" ] + myData[ "var2" ]
the name
2009 Sep 22
3
converting a character vector to a function's input
Hi all, I have been trying to solve this problem and have had no luck so far.
I have numeric vectors VAR1, VAR2, and VAR3 which I am trying to cbind. I also have a character vector "VAR1,VAR2,VAR3". How do I manipulate this character vector such that I can input a transformed version of the character vector into cbind and have it recognize that I'm trying to refer to my numeric
2014 Aug 21
2
pregunta
Buenas noches Javier y José,
Estoy en contra de usar attach(), asi que propongo la siguiente alternativa
con with():
# paquete
require(epicalc)
# los argumentos en ... pasan de epicalc:::cc
# ver ?cc para mas informacion
foo <- function(var1, var2, var3, ...){
or1 <- cc(var1, var2, ...)
or2 <- cc(var1, var3, ...)
list(or1 = or1, or2 = or2)
}
# datos
x <-
2010 Dec 20
2
Turning a Variable into String
I would like to know how to turn a variable into a string. I have tried
as.symbol and as.name but it doesnt work for what I'd like to do
Essentially, I'd like to feed the function below with two variables. This
works fine in the bit working out number of elements in each variable.
In the print(sprintf("OK with %s and %s\n", var1, var2)) line I would like
var1 and var2 to be
2006 Jul 20
3
Permutation Distribution
Hallo
Is there an elegant way to do the following:
Dataset consists of 2 variables: var1: some measurements, and var2: a grouping variable with two values, 1 and 2.
There are (say) 10 measurements from group 1 and 15 measurements from group 2.
The idea is to study the permutation distribution of mean(group 1) * mean(group2).
One way would be to permute 1s and 2s and select the corresponding