[R] Splitting and arranging in ascending order

Rui Barradas ruipbarradas at sapo.pt
Wed Jun 3 17:21:35 CEST 2015


Hello,

Try the following.


tmp <- strsplit(as.character(my_dat$instrument), "_")
tmp <- t(as.data.frame(tmp))
tmp <- data.frame(instrument = tmp[,1],  sr_no = as.integer(tmp[, 2]), 
my_dat$mtm_value)
result <- tmp[order(tmp[, 1], tmp[, 2]), ]
rm(tmp)
rownames(result) <- NULL
result


Hope this helps,

Rui Barradas

Em 03-06-2015 10:09, Amelia Marsh escreveu:
> Dear R forum
>
> I have a data (actually its a big data and I am only giving part of my interest) as
>
> my_dat = data.frame(instrument = c("EQ_0", "EQ_1", "EQ_10", "EQ_100", "EQ_2", "EQ_20", "IRS_0", "IRS_1", "IRS_10", "IRS_100", "IRS_2", "IRS_20"), mtm_value = c(23, 63, 8, 44, 68, 11, 83, 56, 73, 92, 14, 7))
>
>> my_dat
> instrument mtm_value
> 1        EQ_0     23
> 2        EQ_1     63
> 3       EQ_10    8
> 4      EQ_100   44
> 5        EQ_2     68
> 6       EQ_20    11
> 7       IRS_0    83
> 8       IRS_1    56
> 9      IRS_10   73
> 10    IRS_100        92
> 11      IRS_2   14
> 12     IRS_20   7
>
> I need to split the first column and arrange the output in the ascending order as shown below :
>
>    instrument sr_no   mtm_value
>
> 1          EQ          0      23
> 2          EQ          1      63
> 3          EQ          2      68
> 4          EQ         10       8
> 5          EQ         20      11
> 6          EQ        100      44
> 7         IRS         0      83
> 8         IRS         1      56
> 9         IRS         2      14
> 10        IRS       10      73
> 11        IRS       20       7
> 12        IRS      100      92
>
> I tried to use gsub, strsplit  but doesn't give me the required output.
>
> Kindly guide
>
> Regards
>
> Amelia
>
> ______________________________________________
> R-help at r-project.org mailing list -- To UNSUBSCRIBE and more, see
> https://stat.ethz.ch/mailman/listinfo/r-help
> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
> and provide commented, minimal, self-contained, reproducible code.
>



More information about the R-help mailing list