0

I am trying to sort the following data frame by the values in the period column.enter image description here

the code that I am using is as follows :

data = read.csv("inputSample.csv")
datasub = subset(data,data$Period<41 & data$Period>0)
write.csv(datasub,"period+.csv")
new = read.csv("period+.csv")

sub = subset(new,new$NumberOfClaims>0)
sub1 = subset(new,new$NumberOfClaims==0)

opr <- function(set)
{
  return((set$LossAmt * set$SimulationCount)/set$NumberOfClaims)
}

operated = data.frame( sub$LoanID,opr(sub), sub$EndingBalance, sub$BalanceInClaims, sub$Period)
operated = operated[order("sub.Period")]
print(operated)

however the code above simply returns the values of the first column in the dataframe that too in an unsorted order. I have tried using with() and other ways but none of them seem to work. Please help me out. Thanks

camille
  • 16,432
  • 18
  • 38
  • 60
AshLove
  • 1
  • 2
  • You're missing a comma--you need `operated = operated[order("sub.Period"), ]`. (You are using the "rows" argument of `[`, which requires you to leave the "columns" argument blank---but it must be there.) – Gregor Thomas Feb 11 '20 at 15:52
  • @Gregor-reinstateMonica thank you for your reply, but doing that simply returns the first row – AshLove Feb 11 '20 at 15:54
  • 1
    Whoops - you also need to tell it where `sub.Period` comes from (since you're not using `with`), either `operated = operated[order(operated[["sub.Period"]]), ]` or `operated = operated[order(operated$sub.Period), ]` – Gregor Thomas Feb 11 '20 at 16:01
  • @Gregor-reinstateMonica thanks again, the sub.Period is from a result of the subset() function acting on a dataframe that I got by reading a .csv file. Trying the it the first way also returns the first row and trying it the second way returns the first item of the first colum :? – AshLove Feb 11 '20 at 16:15
  • I'm confused then. I thought `sub.period` is a column of `operated`, and you want to sort `operated` by that column? What are the column names of `operated`---if you want to sort by one of them, replace "sub.period" in my code by whatever column name you want to sort by. Look at `names(operated)` if you don't know the names. – Gregor Thomas Feb 11 '20 at 16:18
  • If I'm misunderstanding and "sub.period" only exists in a different data frame, then please be a bit more specific by how you want to order, and make your example **reproducible**. The picture of your data gives an idea, but we can't run any of your code because we don't have your CSV files. Use `dput()` to give us 5-10 rows of any relevant data frames, and we can go from there. [See the FAQ on writing reproducible examples in R](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example) for guidance on creating reproducible examples. – Gregor Thomas Feb 11 '20 at 16:20
  • @Gregor-reinstateMonica sorry for the confusion. sub.operated is a column name in operated, if you see the image I have attached it shows there. – AshLove Feb 11 '20 at 16:29
  • 1
    Well--I can't really tell more without a reproducible example. I'd be happy to take a look if you post enough data for me to run the code. `dput(operated[1:10, ]))` should be good. – Gregor Thomas Feb 11 '20 at 16:33
  • To be clear, we can certainly help with the problem, but something is confusing about your data, and we can't tell from a picture. Create your `operated` data frame, enter `dput(operated[1:10, ]))` into the R console, and copy/paste the result into this question, we'll get a copy/pasteable version of your data frame that we can investigate and test. – Gregor Thomas Feb 11 '20 at 20:06

0 Answers0