[R] Add rows to dataframe by split values
arun
smartpink111 at yahoo.com
Thu Feb 20 18:36:46 CET 2014
Hi,
Try:
library(stringr)
res <- transform(dat[rep(1:nrow(dat),str_count(dat$Result,"-")+1),],Result=unlist(strsplit(as.character(dat$Result)," - ")),Batch=ave(Batch,Batch,FUN=function(x) paste0(x,letters[seq_along(x)])))
row.names(res) <- 1:nrow(res)
# Based on the expected results, it could be also:
res1 <- transform(dat[rep(1:nrow(dat),str_count(dat$Result,"-")+1),],Result=unlist(strsplit(as.character(dat$Result)," - ")),Batch=ave(Batch,Batch,Test,FUN=function(x) paste0(x,letters[seq_along(x)])))
row.names(res1) <- 1:nrow(res1)
A.K.
Hi,
I have a dataframe with some data from experiments:
Batch Test Result LL ......(many more columns, all the same within Batch/Test)
1 A 6,5-6,7 6,00 .....
1 B 95,1-95,3 95,00 .....
2 A 6,5-6,7 6,00 .....
...
Some results have double values, instead of 1 single numeric value, all separated by "-".
But now I want a dataframe with only 1 value per Batch/Test, and batches with double values should be renamed to 1a, 1b, ...
So
Batch Test Result LL ......(many more columns, all the same within Batch/Test)
1a A 6,5
1b A 6,7 6,00 .....
1a B 95,1 95,00 .....
1b B 95,3 95,00 .....
2a A 6,5 6,00 .....
2b A 6,7 6,00 .....
...
I tried with by and strsplit, but couldn't figure it out.
Any help would be appreciated, example data below.
Bart
dat <-
structure(list(Batch = c(1L, 1L, 2L, 2L, 3L, 3L, 4L, 4L, 4L,
4L, 4L, 4L), Test = structure(c(1L, 2L, 1L, 2L, 1L, 2L, 1L, 2L,
1L, 2L, 2L, 2L), .Label = c("A", "B"), class = "factor"), Result = structure(c(12L,
4L, 8L, 6L, 11L, 5L, 10L, 2L, 9L, 3L, 1L, 7L), .Label = c("6,604 - 6,59 - 6,585 - 6,588",
"6,61", "6,625 - 6,607", "6,675", "6,71 - 6,74", "6,76 - 6,75",
"6,79 - 6,80", "98,28 - 99,14", "98,52", "99,05 - 99,15", "99,74 - 98,19",
"99,77 - 99,62"), class = "factor"), X = c(NA, NA, NA, NA, NA,
NA, NA, NA, NA, NA, NA, NA), LL = structure(c(2L, 1L, 2L, 1L,
2L, 1L, 2L, 1L, 2L, 1L, 1L, 1L), .Label = c("6,00", "95"), class = "factor")), .Names = c("Batch",
"Test", "Result", "X", "LL"), class = "data.frame", row.names = c(NA,
-12L))
More information about the R-help
mailing list