I have dfone that has 12 columns:
df<-read.table(header=T,text="V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12
A01 10378809 10379882 Contig1401|m.3412 101 - 10378809 10379882 255,0,0 1 1073 0
A01 10469105 10469293 Contig1755|m.4465 48 + 10469105 10469293 255,0,0 2 188 0
A01 10469429 10469630 Contig1755|m.4465 5 + 10469429 10469630 255,0,0 NA 201 0")
First I want to group them using contig, and then generate the following values ββfor the 12th column. I figured out how to do this with help dplyr, but I have some errors.
as.data.frame(df %.% group_by(V4) %.% summarise(V12=apply(df[2], 2, function(x)x-x[1])))
Error:
Error in summarise_impl(.data, named_dots(...), environment()) :
attempt to use zero-length variable name.
For each group, I want to subtract the second value from the 1st value from the second column. I can do it easily if there are only 2 lines (max-min), but if there are more than 2 lines, I will skip the middle lines.
So, I thought I would write a function and paste in dplyr, but it seems like I cannot use my own function with dplyr.
Here is the final result I need:
V1 V2 V3 V4 V5 V6 V7 V8 V9 V10 V11 V12
1 A01 10378809 10379882 Contig1401|m.3412 101 - 10378809 10379882 255,0,0 1 1073 0
2 A01 10469105 10469293 Contig1755|m.4465 48 + 10469105 10469293 255,0,0 2 188 0
3 A01 10469429 10469630 Contig1755|m.4465 5 + 10469429 10469630 255,0,0 NA 201 324