Dear list,
I'm trying to add a new dim to a multidimensional array. My array looks
like this
a1 <- array(1:8, c(2, 2, 2))
dimnames(a1) <- list(A = c("A1", "A2"),
B = c("B1", "B2"),
D = c("D1", "D2"))
I would like to add a new dim 'group' with the valu
Hello,
I am trying to get a new vector 'x1' based on the not NA-values in
column 'a' and 'b'. I found a way but I am sure this is not the best
solution. So any ideas on how to "optimize" this would be great!
m <- factor(c("a1", "a1", "a2", "b1", "b2", "b3", "d1", "d1"), ordered
= TRUE)
df
Hello,
I am trying to get a new vector 'x1' based on the not NA-values in
column 'a' and 'b'. I found a way but I am sure this is not the best
solution. So any ideas on how to "optimize" this would be great!
m <- factor(c("a1", "a1", "a2", "b1", "b2", "b3", "d1", "d1"), ordered
= TRUE)
df
"A2", "A3"), each=3),
water = sample(c(100:200), 9),
tide = sample(c(-10:+10), 9))
100 * tail(df1$tide, -3) / head(df1$water, -3)
I hope it helps.
Best,
Dimitris
On 10/26/2011 12:02 PM, Patrick Hausmann wrote:
Dear R users,
It may be very simple but it is being difficult
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),
Dear R folks,
I am doing some calculations over an array using sweep and apply.
# Sample Data (from help 'addmargins')
Aye <- sample(c("Yes", "Si", "Oui"), 177, replace = TRUE)
Bee <- sample(c("Hum", "Buzz"), 177, replace = TRUE)
Sea <- sample(c("White", "Black", "Red", "Dead"), 177, replace = T
Dear list,
I would like to expand a DF with all the missing levels of a variable.
a <- c(2,2,3,4,5,6,7,8,9)
a.cut <- cut(a, breaks=c(0,2,6,9,12), right=FALSE )
(x <- data.frame(a, a.cut))
# In 'x' the level "[0,2)" is "missing".
AddMissingLevel <- function(xdf) {
xfac <- factor( c("[0,2)
Dear list,
I would like to do some calculation using different grouping variables.
My 'df' looks like this:
# Some data
set.seed(345)
id <- seq(200,400, by=10)
ids <- sample(substr(id,1,1))
group1 <- rep(1:3, each=7)
group2 <- rep(1:2, c(10,11))
group3 <- rep(1:4, c(5,5,5,6))
df <- data.frame(
Hi,
I think you could also use this way (via array, see
http://r.789695.n4.nabble.com/apply-over-list-of-data-frames-td3057968.html)
b <- list()
b[[1]] = matrix(1:4, 2, 2)
b[[2]] = matrix(10:13, 2, 2)
b[[3]] = matrix(20:23, 2, 2)
b.a <- array(unlist(b), dim=c(2, 2, 3))
(b.mean <- apply(X = b.
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 ther
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 fo
Hi André,
try this:
df1 <- data.frame(x1 = rep(1:3, each=3), x2=letters[1:9])
dfs <- split(df1, df1$x1)
df2 <- data.frame(sapply(dfs, FUN="[[", "x2"))
colnames(df2) <- paste("d", unique(df1$x1), sep="")
df2
HTH
Patrick
Am 06.02.2011 12:13, schrieb André de Boer:
Hello,
Can someone give me
"k.4", "f.9", "f.5"))
x1 <- within(df[df$A ==3, ], {
B <- 5
K <- gsub("f","m", K)
})
x2 <- transform(df[df$A==3, ], B = 5, K = gsub("f","m", K))
Thanks
Patrick
Am 16.01.2011 15:13, schrieb Patrick Hausma
Dear all,
for each A == 3 in 'df' I would like to change the variables B and K.
My result should be the whole df and not the subset (A==3)...
df <- data.frame(A = c(1,1,3,2,2,3,3),
B = c(2,1,1,2,7,8,7),
K = c("a.1", "d.2", "f.3",
"a.1", "k
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),
Hi Mathijs,
this should work:
library(maptools)
library(ggplot2)
gpclibPermit()
theme_set(theme_bw())
#setwd("C:\\foo") point to your local dir
# Data: http://thematicmapping.org/downloads/world_borders.php
world.shp <- readShapeSpatial("TM_WORLD_BORDERS-0.3.shp")
# check for region-id - Use "
Hi Paul,
I am using Sweave and MiKTeX and the results are really impressive, but
it's often quite complicated (or impossible) to share the rnw-files with
my colleagues/clients. So it depends with/for whom you are working.
Perhaps as an alternative you could use a simpler markup format e.g.
"Ma
Here are some examples with tapply, aggregate, ddply:
x <- read.table("clipboard", head=TRUE)
with(x, tapply(quantity, identifier, sum))
aggregate(x$quantity, by=list(x$identifier), sum)
aggregate(quantity ~ identifier, data = x, sum)
library(plyr)
ddply(x, .(identifier), summarise, quantity=
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
calcPercent <- functi
Hi Gregory,
is this what you want? Ok, not the most elegant way...
# using 'melt' from the 'reshape' package
library(reshape)
Data <- data.frame(month = 1:12,
x2002 = runif(12),
x2003 = runif(12),
x2004 = runif(12),
x2005 = ru
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(rownam
Am 08.05.2010 15:43, schrieb Joris Meys:
Dear all,
I want to apply a function to list elements, two by two. I hoped that combn
would help me out, but I can't get it to work. A nested for-loop works, but
seems highly inefficient when you have large lists. Is there a more
efficient way of approach
Hello Alfred,
I found the solution from S. Ellison
(https://stat.ethz.ch/pipermail/r-help/2010-May/238158.html) really
inspiring.
Here I am using tail and the library 'plyr':
set.seed(17*11)
d<-data.frame(africa=sample(50, 10),
europe= sample(50, 10),
n.americ
Hi Angelo,
try
x <- structure(list(ID = c("A1", "A1", "A1", "A1", "A1", "A2", "A2",
"A3", "A3", "A3", "A3", "A3"), YEAR = c(2007, 2007, 2007, 2008,
2008, 2007, 2008, 2007, 2007, 2008, 2008, 2008), PROPERTY = c("P1",
"P2", "P3", "P1", "P2", "P5", "P6", "P1", "P3", "P1", "P2", "P6"
), VALUE = c(1,
, 0.35, 0.41, 0.09, 0.10))
library(reshape)
xpropsum <- melt(propsum, id.var="coverClass", variable_name = "Image")
tpropsum <- reshape(xpropsum, timevar="coverClass", idvar="Image",
direction="wide")
colnames(tpropsum) <- sub("value."
Hi David,
you could use a mix of "plyr" and reshape:
# Example datasets
# Input
propsum <- data.frame(coverClass=c("C", "G", "L", "O", "S"),
R209120812=c(NA, 0.49, 0.38, 0.04, 0.09),
R209122212=c(0.05, 0.35, 0.41, 0.09, 0.10))
library(plyr)
xpropsum <
Hi James,
I don't know how to solve it with "tapply" (something with split I
think..), but you could use "plyr" (from Hadley Wickham).
library(plyr)
# Generate some data
set.seed(321)
myD <- data.frame(
Place = sample(c("AWQ","DFR", "WEQ"), 10, replace=T),
Light = sample(LETTERS[1:2], 15,
Hi Alex,
try this
mfile <- "c:\\ex01.txt"
nperm <- 12
sDate <- paste("date: ", "2009-12-13", sep="")
sFile <- paste("filename: ", mfile, sep="")
sPerm <- paste("number of permutations: ", nperm, sep="")
mt <- matrix(1:10, 2)
sink(mfile)
cat(sDate, "\n")
cat(sFile, "\n")
cat(sPerm, "\n")
Dear list,
I am trying to make a contingency table with xtabs but I am getting
a 0 where I expect a 'NA'. Here is a simple example:
options(stringsAsFactors = FALSE)
rn <- LETTERS[1:4]
df1 <- data.frame(r07 = rep(rn, each=4),
r08 = rep(rn, 4), value = 1:16)
xtabs(value ~ r07 +
Dear list,
# I have a DF like this:
sleep$b <- c(rep(8,10), rep(9,10))
sleep$me <- with(sleep, ave(extra, group, FUN = mean))
sleep
# I would like to create a new variable
# holding the b-th value of group 1 and 2.
# This is not what I want, it takes always the '8' from group '1'
# and not t
Dear list,
I have two things I am struggling...
# First
set.seed(123)
myD <- data.frame( Light = sample(LETTERS[1:2], 10, replace=T),
Feed = sample(letters[1:5], 20, replace=T),
value=rnorm(20) )
# Mean for Light
myD$meanLight <- unlist( lapply( myD$Ligh
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 =
Dear list,
with the code below I produce the right graph, but the colours of the
legend are different from the colours of the graph. The colours of the
graph are the desired colours.
Thanks for any help.
Patrick
library(lattice)
pal1 <- rgb(196, 255, 255, max = 255)
pal2 <- rgb( 0, 35,
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 " "45" " t"
Thanks for any tipp
Pat
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.1
Dear list,
I have a dataframe like this:
w <- c(1.2, 1.34, 2.34, 3.12, 2.43, 1.99, 2.01, 2.23, 1.45, 1.59)
g <- rep(c("a", "b"), each=5)
df <- data.frame(g, w)
df
df
gw
1 a 1.20
2 a 1.34
3 a 2.34
4 a 3.12
5 a 2.43
6 b 1.99
7 b 2.01
8 b 2.23
9 b 1.45
10 b 1.59
Using tapply to
Dear list,
I'm still trying to calculate the sd for V2 for
each group in V1 if V3 is '0':
> x
V1 V2 V3
1 A01 2.40 0
2 A01 3.40 1
3 A01 2.80 0
4 A02 3.20 0
5 A02 4.20 0
6 A03 2.98 1
7 A03 2.31 0
8 A04 4.20 0
# Work
x$vmean <- ave(x$V2, x$V1, x$V3 == 0, FUN = mean)
# Work
x$vsd2 <-
Dear list,
I want to calculate the standard deviation using
'ave' on two different DFs.
In the first DF M1 has only 1 level:
> str(x)
'data.frame': 18 obs. of 3 variables:
$ M1: Factor w/ 1 level "A03": 1 1 1 1 ...
$ M2: num 2.76 2.93 3.06 3.07 3.12 ...
$ M3: Factor w/ 2 levels "Ausgew
38 matches
Mail list logo