Displaying 20 results from an estimated 4000 matches similar to: "How to read a file inside a function?"
2012 Jul 24
3
Collapsing a vector/data-frame based on the previous values
Hello
I have a data frame like this:
dput(states)
structure(list(Date = c("24/07/2012", "25/07/2012", "26/07/2012",
"27/07/2012", "28/07/2012", "24/07/2012", "25/07/2012", "26/07/2012",
"27/07/2012", "28/07/2012"), State = c(1L, 1L, 1L, 1L, 1L, -1L,
-1L, -1L, 1L, -1L)), .Names = c("Date",
2012 Jul 14
2
writing data into files whose names are in a vector
GuRus
How do I use the write function (or write.table or write.csv) to achieve
the following please?
age=c(32,37,39)
names=c("john","peter","jake")
I would like create in a directory 3 files each named as john.csv,peter.csv
and jake.csv and each file have data from the age vector. That is jon.csv
will contain 32, peter.csv will contain 37 and jake.csv will contain
2012 Apr 22
2
difficulty in Formatting time series data
Dear R-Gurus
I have a data frame (from CSV file) which has its first column called Date.
The Date is in the format mm/dd/yyyy. I was trying to get the weekday for
these dates and I tried using wday() and day.of.week() functions and both
of them gave me precisely the wrong answers. I think the issue lies in the
proper formatting of dates. The class of this column is a factor class and
hence I
2012 Jul 08
3
Help in Optimization of a function
guRus!
I have a function f = exp(x^2-y+(1/z))
Also, x can take values from 1 to 37, y from 2 to 20 and Z from -13 to 51.
How can I find the maximum of f using any of the optimization functions
please?
Is there a way to store the possible values of x, y and Z in a single
variable like in a List or in a multi-dimensional array?
Thanks for your help
Raghu
[[alternative HTML version deleted]]
2012 Jun 19
1
Profit calculation
Dear guRus
Say I have a price vector, P which is P = c(20,50,40,50) for four
consecutive days.
My initial equity is say $100 and I re-invest the profits made in each
transaction so my equity curve increases.
If I go Long on day 1, short and short on day 2, long and long on day 3 and
finally short on day 4, how do I calculate the total profits using R?
Basically I stay invested at all times.
I
2010 Aug 06
3
How to apply apply?!
guRus
I have say a dataframe, d and I wish to do the following:
1) For each row, I want to take one particular value of the row and multiply
it by 2. How do I do it. Say the data frame is as below:
OPEN HIGH LOW CLOSE 1931.2 1931.2 1931.2 1931.2 0 0 0 999.05 0 0 0 1052.5
0 0 0 987.8 0 0 0 925.6 0 0 0 866 0 0 0 1400.2 0 0 0 754.5 0 0 0 702.6 0 0 0
653.25 0 0 0 348 0 0 0 801 866.55 866.55
2010 Jul 28
3
how to code it??
Hi
I have say a large vector of 3500 digits. Initially the digits are 0s and
1s. I need to check for a rule to change some of the 0s to -1s in this
vector. But once I change a 0 to -1 then I need to start applying the rule
to change the next 0 only after I see the next 1 in the vector.
Say for example x = (0,0,0,0,0,0,0,1,0,0,1,1,0,0,1,0,0,0,1)
I need to traverse from the 9th element to the last
2012 Oct 25
5
trying ti use a function in aggregate
Hi -I am using R v 2.13.0. I am trying to use the aggregate function to
calculate the percent at length for each Trip_id and CommonName. Here is a
small subset of the data.
Trip_id Vessel CommonName Length Count
1 230 Sunlight Shad,American 19 1
2 230 Sunlight Shad,American 20 1
3 230 Sunlight Shad,American 21
2012 Jul 10
1
Help with vectors and rollapply
Hello
I have a vector a =(-2,0,0,0,1,0,0,3,0,0,-4)
I want to replace all zeros into previous non-zero state. So for instance the above vector should be converted into:
a= (-2,-2,-2,-2,1,1,1,3,3,3,-4)
I tried many things and finally concluded that probably(?) rollapply may be the best way?
I tried
f= function(x){
ifelse(x==0,Lag(x),x)
}
And then, rollappy(a,1,f) and that
2011 Jan 06
8
Accessing data via url
# Can anyone suggest why this works
datafilename <- "http://personality-project.org/r/datasets/maps.mixx.epi.bfi.data"
person.data <- read.table(datafilename,header=TRUE)
# but this does not?
dd <- "https://sites.google.com/site/jrkrideau/home/general-stores/trees.txt"
treedata <- read.table(dd, header=TRUE)
2006 Aug 23
1
glm inside one self-defined function
Hi list,
I've searched in R-help and found some related discussions but still could
not understand this type of error. My own function is pretty complex, so I
would not put it here, but the basic algorithm is like this:
myfun<-function(k){
mydata<-...#by someway I create a data frame
mymodel<-glm(y~.,family=binomial(),data=mydata)
...#some other stuff
}
as I execute this
2010 Jul 28
2
finding the next highest number in an array
Hi
I have a sorted array ( in ascending order) and I want to find the subscript
of a number in the array which is the the next highest number to a given
number. For example,if I have 67 as a given number and if I have a vector
x=c(23,36,45,62,79,103,109), then how do I get the subscript 5 from x (to
get 79 which is the next highest to 67) without using a for loop?
Thx
--
'Raghu'
2010 Jul 06
4
Adding two files into one and vlookup
I have two files with dates and prices in each. The number of rows in each of
them will differ. How do I create a new file which contains data from both
these files? Cbind and merge are not helpful. For cbind because the rows are
not the same replication occurs. Also if I have similar data how do I write
a vlookup kind of function? I am giving an example below:
Say Price1 file contains the
2011 Mar 23
2
) Error in eval(expr, envir, enclos) : object '' not found
> datafilename="E:/my documents/r/sex/bysex1.csv"
> data.sex=read.table(datafilename,header=T)
> data.sex
y.sex.age.region.c.n
1 1980,F,A,N,-18.15,13.61
2 1980,F,A,N,-18.61,13.04
3 1980,F,A,N,-18.81,12.32
4 1990,F,A,N,-21.12,11.7
5 1990,F,A,N,-20.77,11.58
6 1990,F,A,N,-21.6,13.34
7 1990,F,A,N,-21.78,12.6
> model.anova<-aov(c~age*sex,data=data.sex)
2010 Jul 12
2
a small puzzle?
I know the following may sound too basic but I thought the mailing list is
for the benefit of all levels of people. I ran a simple if statement on two
numeric vectors (news1o and s2o) which are of equal length. I have done an
str on both of them for your kind perusal below. I am trying to compare the
numbers in both and initiate a new vector s as 1 or 0 depending on if the
elements in the arrays
2009 Apr 18
3
need help with tinc windows service
hi
im a newbie to networking and vpns.so plz pardon my mistakes.
i installed tinc 1.0.9 and followed the tinc setup instructions as in
the tinc-windows example.
after i hav started the tinc service with tincd -n command the service
starts and then stops abrupty giving the message
" the tinc.test service on the local computer has started and then
stopped.Some services stop automatically if
2010 Sep 20
2
interpreting one-way anova tables
Hi, I am trying to reconcile anova table in R (summary(lm)) with individual t.test.
datafilename="http://personality-project.org/R/datasets/R.appendix1.data"
data.ex1=read.table(datafilename,header=T) #read the data into a table
summary(lm(Alertness~Dosage,data=data.ex1))
gives:
Call:
lm(formula = Alertness ~ Dosage, data = data.ex1)
Residuals:
Min 1Q Median 3Q Max
2010 May 10
1
how to get p-value from ave
Hi there,
I checked google for aov. usually one uses summary to see whether the p-value is small.
but I want to put aov in my script. how can I get the p-value, (0.1115, 0.6665, 0.6665 in the following example)?
thanks
YU
> datafilename="http://personality-project.org/r/datasets/R.appendix2.data"
> data.example2=read.table(datafilename,header=T)
> aov.ex2 =
2004 Jul 16
3
sas to r
I would be incredibly grateful to anyone who'll help me translate some
SAS code into R code.
Say for example that I have a dataset named "dat1" that includes five
variables: wshed, site, species, bda, and sla. I can calculate with the
following SAS code the mean, CV, se, and number of observations of
"bda" and "sla" for each combination of
2012 Jun 10
1
compute Mcdonald's omega ω
Dear all
I am a newbie to R and I would appreciate it very much if someone can
give me some advice on this.
Please note that I am not a programmer so some of the questions might
sound really stupid.
I would like to compute McDonald's omega calculation using R, I'm
aware I can use the omega function in the psych package.
But I'm really not sure how to do it.
I have read these two