Displaying 20 results from an estimated 8000 matches similar to: "Collapsing a vector/data-frame based on the previous values"
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]]
2010 Aug 06
2
How to read a file inside a function?
Hi
I wish to read a file from my local directory from inside a function. I am
passing the filename as the argument but this does not work.
Say for example
function(dat)
{
dat1=read.csv("D:\\dat.csv",header=TRUE)
}
If I call funtion(dat) I get the following error. 'Intuitively' i understand
this is a mistake but how do I overcome this and how can I read a file name
passed as an
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
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'
2012 Oct 08
3
How to count combinations
Dear all,
Need a help. I would like to count combination of two columns:
structure(list(V1 = structure(c(4L, 1L, 1L, 1L, 1L, 1L, 1L, 1L,
1L, 1L, 1L, 1L, 1L, 2L, 2L, 3L, 3L, 3L, 3L, 3L, 3L, 3L), .Label = c("23",
"55", "67", "Env"), class = "factor"), V2 = structure(c(9L, 7L,
7L, 7L, 2L, 1L, 11L, 11L, 11L, 11L, 5L, 6L, 6L, 3L, 8L, 2L, 1L,
4L, 4L,
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
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
2012 Nov 14
2
indexing for Wilcoxon test (take 2)
Hi everyone,
I've been asked to run a number of Wilcoxon tests on some behavioral data
(below is a sample). They want me to compare each trial to the first
trial, considered the control trial. I know I can use brackets to index and
select, for example, trial 1 vs. trial 4 (not exactly sure how to set that
up either) but it would save me a ton of time if there was a way to do all
the
2010 Jul 12
6
in continuation with the earlier R puzzle
When I just run a for loop it works. But if I am going to run a for loop
every time for large vectors I might as well use C or any other language.
The reason R is powerful is becasue it can handle large vectors without each
element being manipulated? Please let me know where I am wrong.
for(i in 1:length(news1o)){
+ if(news1o[i]>s2o[i])
+ s[i]<-1
+ else
+ s[i]<--1
+ }
--
2013 Jul 18
1
Bland Altman summary stats for all column combinations
Hello,
I have the following data.frame
structure(list(Study = structure(c(1L, 2L, 3L, 4L, 5L, 6L, 7L,
8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 1L,
2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L, 15L,
16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L, 5L, 6L, 7L, 8L, 9L, 10L,
11L, 12L, 13L, 14L, 15L, 16L, 17L, 18L, 19L, 1L, 2L, 3L, 4L,
5L, 6L, 7L, 8L, 9L, 10L, 11L, 12L, 13L, 14L,
2017 Dec 14
3
help with recursive function
If you are trying to understand why the "stopifnot" condition is met you
can replace it by something like:
if ( any(dat2$norm_sd >= 1) )
browser()
This will put you in a debugging session where you can examine your
variables, e.g.
> dat$norm_sd
HTH,
Eric
On Thu, Dec 14, 2017 at 5:33 PM, Eric Berger <ericjberger at gmail.com> wrote:
> The message is coming from
2012 Aug 10
2
Regular Expressions + Matrices
Hi all,
My code looks like the following:
inname = read.csv("ID_error_checker.csv", as.is=TRUE)
outname = read.csv("output.csv", as.is=TRUE)
#My algorithm is the following:
#for line in inname
#if first string up to whitespace in row in inname$name = first string up
to whitespace in row + 1 in inname$name
#AND ID in inname$ID for the top row NOT EQUAL ID in inname$ID for the
2017 Dec 14
0
help with recursive function
Eric: I will try and see if I can figure out the issue by debugging as you suggested. I don?t know why my code after stopifnot is not getting executed where I like the code to run the funlp2 function when the if statement is TRUE but when it is false, I like it to keep running until the stopifnot condition is met.
When the stopifnot condition is met, I like to get the output from if statement
2017 Dec 14
2
help with recursive function
Hi, I accidently left out few lines of code from the calclp function. Updated function is pasted below.
I am still getting the same error ?Error: !(any(data1$norm_sd >= 1)) is not TRUE?
I would appreciate any help.
Nilesh
dput(calclp)
function (dataset)
{
dat1 <- funlp1(dataset)
recursive_funlp <- function(dataset = dat1, func = funlp2) {
dat2 <- dataset %>%
2017 Oct 18
2
creating tables with replacement
Hi R User,
I am new in R and trying to create tables with selecting rows randomly (but
with replacement) for each group but each group should have same number as
original. Is it possible to create it using the following example data set?
Your help is highly appreciated.
dat1<-structure(list(RegionA = structure(c(1L, 1L, 2L, 3L, 3L, 4L,
5L, 5L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L, 6L),
2017 Dec 14
2
help with recursive function
My own typo ... whoops ...
!( any(dat2$norm_sd >= 1 ))
On Thu, Dec 14, 2017 at 3:43 PM, Eric Berger <ericjberger at gmail.com> wrote:
> You seem to have a typo at this expression (and some others like it)
>
> Namely, you write
>
> any(!dat2$norm_sd) >= 1
>
> when you possibly meant to write
>
> !( any(dat2$norm_sd) >= 1 )
>
> i.e. I think your !