Displaying 20 results from an estimated 30000 matches similar to: "Question on assignment"
2013 Mar 22
2
A question on function return
Hello again,
Let say I have following user defined function:
fn <- function(x, y) {
Vec1 <- letters[1:6]
Vec2 <- 1:5
return(list(ifelse(x > 0, Vec1, NA), ifelse(y > 0, Vec2, NA)))
}
Now I have following calculation:
> fn(-3, -3)
[[1]]
[1] NA
[[2]]
[1] NA
> fn(3, -3)
[[1]]
[1] "a"
[[2]]
[1] NA
Here I can not understand why in the second case, I get
2005 Feb 01
2
assign connections automatically
Hi all,
I am trying to create a function that will open connections to all
files of
one type within the working directory.
I've got the function to open the connections, but I am having a
bugger of a
time trying to get these connections named as objects in the workspace.
I am at the point where I can do it outside of the function, but not
inside, using assign. I'm sure I'm
2012 Apr 24
1
Use of optim to fit two curves at the same time ?
Dear list,
Here is a small example code that use optim and optimize in order to fit
two functions.
Is it possible to fit two functions (like those two for example) at the
same time using optim ... or another function in R ?
Thanks
Arnaud
######################################################################
## function 1
x1 <- 1:100
y1 <- 5.468 * x + 3 # + rnorm(100,0, 10)
dfxy <-
2005 Jul 25
1
assignment of matrix
Hi,
I have created a matrix and initially all elements in the matrix was
assigned to NA.Then I want to assign values to some elements of this
matrix. Can I use formation like matrix[i][j] <- 4?if not, how can i do
this? now I have three vectors
y1 y2 y3
3 4 3.1
5 2 4.2
if I want to assign y3's value to matrix, where the value's row and column
should correspond the
2008 Apr 17
1
Help with using 'get' function and variable scope
Hi -
I'm having a really hard time w/understanding R's get function, and would
appreciate any help with this.
Specifically, I'm using a for loop to call a function. I'd like the
function to have access to the variable being incremented in the for-loop,
i.e.
t.fn <- function() return( get( "i" ) )
t.fn2 <- function() {
for ( i in 1:5 )
cat( t.fn(),
2011 Feb 07
1
Seeking help to define s4 method for 'print'
I have got another question on defining a method for print() function for my
s4 class. Here is my class definition as well as what I have done till now:
> setClass("Me", representation(x1 = "numeric", x2 = "numeric", y1 =
"character"))
[1] "Me"
> setMethod("print", "Me", definition=function(x) {
+ cat("Values
2013 Mar 17
3
help with simple function
hello all
I am writing a quite simple script to study dental wear patterns in humans
and I wrote this function
sqrt(var(Y1)+var(Y2))^2-4(var(Y1)*(var(Y2)-cov(Y1,Y2)^2)) but appear this
error message
Error: attempt to apply non-function
alternatively I wrote this
sqrt(var(Y1)+var(Y2)^2)-4[(var(Y1)*(var(Y2)-cov(Y1,Y2)^2))]
but this error message appear
[1] NA
Warning message:
NAs introduced
2008 Jan 29
3
How to get two y-axises in a bar plot?
Hi,
I have measured two response variables (y1, y2) at each treatment level
(x = 0, 1.5 or 3). Now I would like to show the y1 and y2 against x in a
bar plot. However, y1 and y2 differ in scale so I need two y-axises, one
on the left side and one on the right side (and I dont want to
standardize my responses). This is fairly easy if you want to show
points,lines etc, but gets more complicated
2008 Feb 15
1
ggplot2 used in a function - variable scope/environment
Hi everybody!
I'm trying to use ggplot2 to return a plot from a function (so I can add
something or alter it then). Unfortunately, if I add a mapping to a
layer in the function, the variable *name* is stored in the layer,
rather than the variable's *value* - so that after the function returns
the ggplot2-object, it doesn't plot because the variable don't exist in
the
2007 Jan 30
2
Rbind for appending zoo objects
Hi R,
y1 <- zoo(matrix(1:10, ncol = 2), 1:5)
colnames(y1)=c("a","b")
y2 <- zoo(matrix(rnorm(10), ncol = 2), 6:10)
colnames(y2)=c("b","a")
> y1
a b
1 1 6
2 2 7
3 3 8
4 4 9
5 5 10
> y2
b a
6 0.9070204 0.3527630
7 1.2405943 0.8275001
8 -0.1690653 -0.1724976
9 -0.6905223 -1.1127670
10
2011 Aug 15
1
update() ignores object
Hi all,
I'm extracting the name of the term in a regression model that
dropterm specifies as the least significant one, and I'm assigning
this name to an object. However, when I use update(), it ignores this
object. Is there a way I can make it not ignore it? A reproducible
example is below:
> lm(x1~1+y1*y2+y3+y4,data=anscombe)->my.lm
>
2010 Oct 17
1
lattice xyplot - formatting of multiple Y variables when using subgroups
Hi all,
Using xyplot I want to print to Y variables (y1, y2) versus X, conditional
on the group.
How can I obtain a line (type="l") for one relationship (ie. y1 ~ x) and
points (type="p") for the other (y2 ~ x) ?
library(lattice)
# create some sample data
df<-data.frame(group=as.factor(c(rep("a",4), rep("b",4))), # grouping
variable for conditional
2003 Mar 04
4
writing several command line in R console
Hi R lovers
I would like to know how to step to the next line in the R console editor
without breaking the continuity of my code
more clearly : if for example I write a function, so far i have to write the
all code inside on the same line wich may become obscure as the function is
more and more complex.
I would like to do like in the example of the manuels:
>twosam <- function(y1, y2) {
2010 Feb 11
2
Find each time a value changes
Dear List,
I am trying to find each time a value changes in a dataset. The numbers are variables for day vs. night values, so what I am really getting is the daily sunrise and sunset.
A simplified example is the following:
x<-seq(1:100)
y1<-rep(1,10)
y2<-rep(2,10)
y<-c(y1,y2,y1,y1,y1,y2,y1,y2,y1,y2)
xy<-cbind(x,y)
I would like to know each time the numbers change.
Correct
2013 Apr 08
3
Reshaping a table
Hello all,
I have data in the form of a table:
X Y1 Y2
0.1 3 2
0.2 2 1
And I would like to transform in the form:
X Y
0.1 Y1
0.1 Y1
0.1 Y1
0.1 Y2
0.1 Y2
0.2 Y1
0.2 Y1
0.2 Y2
Any ideas how?
Thanks in advance,
IOanna
[[alternative HTML version deleted]]
2008 Feb 27
4
plot y1 and y2 on one graph
Dear all
I have a code like
x<-1:10
y1<-x+runif(10)*2
y2<-seq(0,50,length.out=10)+rnorm(10)*10
par(mfrow=c(1,2))
plot(y1~x)
plot(y2~x)
Now I would like to plot y1 and y2 on the same graph, with its two scales
(y1 on left and y2 on rigth side).
Any help are welcome.
Kind regards
Miltinho
Brazil
[[alternative HTML version deleted]]
2011 Jan 19
2
Reshape
Hi - I'm up against a complicated reshape problem. I have data of the form
X1,Y1,hr1,hr2,hr3
X1,Y2,hr1,hr2,hr3
X1,Y3,hr1,hr2,hr3
X2,Y1,hr1,hr2,hr3
X2,Y2,hr1,hr2,hr3
X2,Y3,hr1,hr2,hr3
where X and Y are factors and the hr(1,2,3) are values. I need it as
,X1, X2
Y1,hr1,hr1
Y1,hr2,hr2
Y1,hr3,hr3
Y2,hr1,hr1
Y2,hr2,hr2
Y2,hr3,hr3
..,
Any hints? I've been at it for hours.
p
--
View
2006 Oct 27
1
Using data and subset arguments in user-defined functions
Dear list,
A while ago, I posted a question asking how to use data or subset
arguments in a user-defined function. Duncan Murdoch suggested the
following solution in the context of a data argument:
data <- data.frame(a=c(1:10),b=c(1:10))
eg.fn <- function(expr, data) {
x <- eval(substitute(expr), envir=data)
return(mean(x))
}
eg.fn(a,data)
I've
2006 Mar 10
1
add trend line to each group of data in: xyplot(y1+y2 ~ x | grp...
Although this should be trivial, I'm having a spot of trouble.
I want to make a lattice plot of the format y1+y2 ~ x | grp but then fit a
lm to each y variable and add an abline of those models in different colors.
If the xyplot followed y~x|grp I would write a panel function as below, but
I'm unsure of how to do that with y1 and y2 without reshaping the data
before hand. Thoughts
2011 Aug 02
1
My R code is not efficient
Dear R users,
I have two n*1 integer vectors, y1 and y2, where n is very very large.
I'd like to compute
elbp = 4^(y1) * 5^(y2) * sum_{i=0}^{max(y1, y2)} [{ (y1-i)! * (i)! *
(y2-i)! }^(-1)];
that is, I need to compute "elbp" for each (y1, y2) pair.
So I made R code like below, but I don't think it's efficient
Would you plz tell me how to avoid this "for"