Hi!
How about this:
--- snip --
for (i in 1:(length(split_str)-1)) {
assign(paste("DF",i,sep=""),DF[
c((which(DF$name==split_str[i])+1):(which(DF$name==split_str[i+1])-1)),
])
}
--- snip ---
'assign' creates for each subset a new data.frame DFn, where n ist a
count (1,2,...).
But note: i
Forgot to take care of the boundary conditions:
# revised data.frame to take care of boundary conditions
DF = data.frame(name = c('b', 'a','v','z', 'c','d'), val = 0); DF
## name val
## 1b 0
## 2a 0
## 3v 0
## 4z 0
## 5c 0
## 6d 0
split_str = c('a', 'c')
# If
...
yes, but note that:
which(data[[col]] %in% s
can be replaced directly by match:
match(data[[col]], s)
Corner cases (nothing matches, etc.) would also have to be checked and
probably should sort the matched row numbers for safety.
Cheers,
Bert
Bert Gunter
"The trouble with having an open
DF = data.frame(name = c('a', 'v', 'c'), val = 0); DF
## name val
## 1a 0
## 2v 0
## 3c 0
split_str = c('a', 'c')
# If we assume that the values in split_str are ordered in the same order
as in the dataframe, then this might work.
offsets <- match(split_str, DF$name)
# Since yo
Hello,
Maybe something like the following.
splitDF <- function(data, col, s){
n <- nrow(data)
inx <- which(data[[col]] %in% s)
lapply(seq_along(inx), function(i){
k <- if(inx[i] < n) (inx[i] + 1):(inx[i + 1])
data[k, ]
})
}
splitDF(DF, "name", split_str)
Hope t
Hi,
I am struggling to split a data.frame as will below scheme :
DF = data.frame(name = c('a', 'v', 'c'), val = 0); DF
split_str = c('a', 'c')
Now, for each element in split_str, R should find which row of DF contains
that element, and return DF with all rows starting from next row of the
corre
Hi Arun,
Sorry for late reply.
I would like to thank you for your pointer. This is what I wanted. Thanks
for your help.
Thanks and regards,
On Sun, Oct 27, 2013 at 10:33 PM, arun wrote:
> Hi,
> DF$Col2
> # [1] a e b b a b c e b a c c e e a c d c c e
> #Levels: a b c d e
>
>
> "b" is not foun
Hi,
DF$Col2
# [1] a e b b a b c e b a c c e e a c d c c e
#Levels: a b c d e
"b" is not found in any of the "Grs". Also Gr3 is not presnt in DF$Col2
So, I am not sure whether this works for you.
indx <- 1+ 2*DF$Col2 %in% Gr1 + 4*DF$Col2 %in% Gr2 + 8*DF$Col2 %in% Gr3
indx <- indx[indx>1]
spli
Hi again,
Let say I have following DF:
DF <- structure(list(Col1 = 1:20, Col2 = structure(c(1L, 5L, 2L, 2L,
1L, 2L, 3L, 5L, 2L, 1L, 3L, 3L, 5L, 5L, 1L, 3L, 4L, 3L, 3L, 5L
), .Label = c("a", "b", "c", "d", "e"), class = "factor")), .Names =
c("Col1",
"Col2"), row.names = c(NA, -20L), class = "data
9 matches
Mail list logo