Displaying 20 results from an estimated 10000 matches similar to: "ave and grouping"
2008 May 17
1
tapply and grouping
Hello all,
I have a df like this:
w <- c(1.20, 1.34, 2.34, 3.12, 2.89, 4.67, 2.43,
2.89, 1.99, 3.45, 2.01, 2.23, 1.45, 1.59)
g <- rep(c("a", "b"), each=7)
df <- data.frame(g, w)
df
# 1. Mean for each group
tapply(df$w, df$g, function(x) mean(x))
# 2. Range for each group - fix value 0.15
tapply(df$w, df$g,
function(x)
x[(x > mean(x) -
2002 Jul 03
2
Adding text to a plot
Dear R-users,
again two question...
# Question 1
Adding two lines of text to a plot, I am using:
# -------------------------------
plot(k[,1], k[,2], pch=16,
ylim=range((min(k[,2])-0.2):(max(k[,2])+1)))
a <- paste("Cor.:" ,cor(k[,2],k[,1]))
b <- paste(nrow(k), "Countries")
text(90, max((k[,2])-0.51), a)
text(90, max((k[,2])-0.83), b)
#
2011 Feb 06
3
manipulate dataframe
Hello,
Can someone give me hint to change a data.frame.
I want to split a column in more columns depending on the value of a other
column.
Thanks for the reaction,
Andre
Example:
> dat
x1 x2
1 1 a
2 1 b
3 1 c
4 2 d
5 2 e
6 2 f
7 3 g
8 3 h
9 3 i
in
> dur
d1 d2 d3
1 a d g
2 b e h
3 c f i
[[alternative HTML version deleted]]
2010 Nov 30
1
more flexible "ave"
Hi all,
I would like to calculate the percent of the total per group for this
data.frame:
df <- data.frame(site = c("a", "a", "a", "b", "b", "b"),
gr = c("total", "x1", "x2", "x1", "total","x2"),
value1 = c(212, 56, 87, 33, 456, 213))
df
2011 Oct 26
1
Calculate the difference using ave
Dear R users,
It may be very simple but it is being difficult for me.
I'd like to calculate the difference in percent between to measures.
My data looks like this:
set.seed(123)
df1 <- data.frame(measure = rep(c("A1", "A2", "A3"), each=3),
water = sample(c(100:200), 9),
tide = sample(c(-10:+10), 9))
df1
# What I want to calculate is:
2002 Jun 14
1
data.frame - transform
Hi there,
I have a data.frame (pwt6) which I would like to transform:
country year gdp
MEX 1950 2
MEX 1951 5
BOL 1950 4
BOL 1951 12
ITA 1950 45
ITA 1951 2
This should be the result:
year MEX.gdp BOL.gdp ITA.gdp
1950 2 4 45
1951 5 12 2
Right now I have this code (better - no code):
country.label<-names(table(pwt6$country))
result<-data.frame(year=NULL)
for(i in country.label)
?
2002 Jun 09
1
Logical operators
Hello,
I have a csv-file which looks like:
#### pwt6_r.csv ####
code;year;rgdpch
AGO;1998;1234
ALB;1998;3576
ARG;1998;#NA
SVN;1996;13439
SWE;1996;21492
AGO;1960;#NA
ALB;1960;2345
ARG;1960;4634
#### pwt6_r.csv ####
To import this file i call:
pwt6<-read.csv("d:/pwt6_r.csv",header=T,na.strings="#NA",sep=";")
Now I want to generate a new data.frame which include
2002 Jun 06
3
Problem with get.hist.quote (tseries library)....
Hello,
I am having a problem with the get.hist.quote command (tseries library) in
the Windows version.
This problem is not happening is the Linux version (Mandrake 8.2).
Attached is the error message, for an example included in the help file.
Also the R.Version() details is attached.
Please, do you know if there is a workaround ?
Thanks,
Carlos.
++++++++++++++++++++++++ ERROR MESSAGE
2011 Feb 23
2
list multiplied by a factor / mapply
Dear list,
this works fine:
x <- split(iris, iris$Species)
x1 <- lapply(x, function(L) transform(L, g = L[,1:4] * 3))
but I would like to multiply each Species with another factor:
setosa by 2, versicolor by 3 and virginica by 4. I've tried mapply but
without success.
Any thoughts? Thanks for any idea!
Patrick
2011 Mar 24
1
fraction with timelag
Dear r-help,
I'm having this DF
df <- data.frame(id = 1:6,
xout = c(1234, 2134, 234, 456, 324, 345),
xin= c(NA, 34,67,87,34, NA))
and would like to calculate the fraction (xin_t / xout_t-1)
The result should be:
# NA, 2.76, 3.14, 37.18, 7.46, NA
I am sure there is a solution using zoo... but I don't know how...
Thanks for any help!
Patrick
2008 Aug 30
1
strsplit and regexp
Dear list,
I am trying to split a string using regexp:
x <- "2 Value 34 a-c 45 t"
strsplit(x, "[0-9]")
[[1]]
[1] "" " Value " "" " a-c " "" " t"
But I don't want to lose the digits (pattern), the result
should be:
[[1]]
[1] "2" " Value " "34" " a-c "
2010 Jun 21
1
replace NA-values
Dear list,
I'm trying to replace NA-values with the preceding values in that column.
This code works, but I am sure there is a more elegant way...
df <- data.frame(id = c("A1", NA, NA, NA, "B1",
NA, NA, "C1", NA, NA, NA, NA),
value = c(1:12))
rn <- c(rownames(df[!is.na(df$id),]), nrow(df)+1)
rn <-
2011 Jan 10
1
Using combn
Dear list,
I want to apply the "table" function to every pair of variables in df
and the return should be a list.
setwd(123)
asd <- data.frame(a1=sample(1:4, 20, replace=TRUE),
a2=sample(1:4, 20, replace=TRUE),
a3=sample(1:4, 20, replace=TRUE),
a4=sample(1:4, 20, replace=TRUE))
with(asd, table(a1, a2))
with(asd, table(a1,
2002 Jun 21
1
rbind
Hello,
I am sure this is a trivial question, but I don't get it...
Having a dataframe (b.70) like that:
> b.70[1:4,2:3]
ARG AUS
1960 19041 25949
1961 19675 25451
1962 19302 26463
1963 18121 27644
Now I want to calculate the log for each year and save the
result in a new dataframe called 'x'.
x<- data.frame(w=NULL)
for(i in 1:3)
2009 Jan 13
2
NA-values and logical operation
Dear list,
as a result of a logical operation I want to assign
a new variable to a DF with NA-values.
z <- data.frame( x = c(5,6,5,NA,7,5,4,NA),
y = c(1,2,2,2,2,2,2,2) )
p <- (z$x <= 5) & (z$y == 1)
p
z[p, "p1"] <-5
z
# ok, this works fine
z <- z[,-3]
p <- (z$x <= 5) & (z$y == 2)
p
z[p, "p2"] <-5
z
# this failed... - how
2009 Apr 29
1
Corrupt data frame construction - bug?
Hi useRs,
A recent coding infelicity along these lines
yielded a corrupt data frame.
foo <- matrix(1:12, nrow = 3)
bar <- data.frame(foo)
bar$NewCol <- foo[foo[, 1] == 4, 4]
bar
lapply(bar, length)
> foo <- matrix(1:12, nrow = 3)
> bar <- data.frame(foo)
> bar$NewCol <- foo[foo[, 1] == 4, 4]
> bar
X1 X2 X3 X4 NewCol
1 1 4 7 10 <NA>
2 2 5 8 11
2010 Jun 01
1
data frame manipulation ddply
Dear group,
Here is my data frame:
futures <-
structure(list(DESCRIPTION = c("CORN Jul/10", "CORN Jul/10",
"CORN Jul/10", "CORN Jul/10", "CORN Jul/10", "LIVE CATTLE Aug/10",
"LIVE CATTLE Aug/10", "SUGAR NO.11 Jul/10", "SUGAR NO.11 Jul/10",
"SUGAR NO.11 Jul/10", "SUGAR NO.11
2009 May 26
1
Bug in "$<-.data.frame" yields corrupt data frame (PR#13724)
Full_Name: Steven McKinney
Version: 2.9.0
OS: Mac OS X 10.5.6
Submission from: (NULL) (142.103.207.10)
A corrupt data frame can be constructed as follows:
foo <- matrix(1:12, nrow = 3)
bar <- data.frame(foo)
bar$NewCol <- foo[foo[, 1] == 4, 4]
bar
lapply(bar, length)
> foo <- matrix(1:12, nrow = 3)
> bar <- data.frame(foo)
> bar$NewCol <- foo[foo[, 1] == 4, 4]
2009 Apr 23
2
conditional grouping of variables: ave or tapply or by or???
Dear R Users,
I have the following data frame:
v1 <- c(rep(10,3),rep(11,2))
v2 <- sample(5:10, 5, replace = T)
v3 <- c(0,1,2,0,2)
df <- data.frame(v1,v2,v3)
> df
v1 v2 v3
1 10 9 0
2 10 5 1
3 10 6 2
4 11 7 0
5 11 5 2
I want to add a new column v4 such that its values are equal to the value
of v2 conditional on v3=0 for each subgroup of v1. In the above example,
the
2007 Nov 16
0
Odp: R: ave and quantile
Hi
Patrick Hausmann <c18g at uni-bremen.de> napsal dne 15.11.2007 18:59:06:
> Hello Petr,
>
> one question solved, the next is standing in front of me... If you
> have a minute to look .... great!
>
> > x
> V1 V2 F1
> 1 A 2 0.1552277
> 2 A 3 0.1552277
> 3 A 4 0.1552277
> 4 B 3 0.8447723
> 5 B 2 0.8447723
> 6 C 6 0.2500000