Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Unable to replace values in new FLBiol @mat slot #35

Closed
marchtaylor opened this issue May 4, 2017 · 6 comments
Closed

Unable to replace values in new FLBiol @mat slot #35

marchtaylor opened this issue May 4, 2017 · 6 comments
Assignees

Comments

@marchtaylor
Copy link

marchtaylor commented May 4, 2017

I'm having difficulties trying to replace some NAs in the @mat slot of an FLBiol object (also applies to @fec slot). The error reads "Error in names(lst) <- names(x) : 'names' attribute [1] must be the same length as the vector [0]". As far I can tell, the names of FLQuant objects (original and replacement) are the same, so I don't see where the problem lies.

Here is an example:

library(FLCore)
data(ple4.biol)

# replacement fails
tmp <- replace(ple4.biol@mat$mat, ple4.biol@mat$mat==0, 0.1)
ple4.biol@mat$mat <- tmp

# names are identical
names(ple4.biol@mat$mat)
names(tmp)

# same operation in another slot works
tmp <- replace(ple4.biol@spwn, ple4.biol@spwn==0, 0.1)
ple4.biol@spwn <- tmp
@marchtaylor marchtaylor changed the title Unable to replace values in new FLBiol@mat slot Unable to replace values in new FLBiol @mat slot May 4, 2017
@marchtaylor
Copy link
Author

possibly related: window() and trim() do not affect the FLBbiol slots @mat, @fec, and @rec .

@marchtaylor
Copy link
Author

I think I have found a solution:

library(FLCore)
data(ple4.biol)

# these work
tmp <- replace(ple4.biol@mat$mat, ple4.biol@mat$mat==0, 0.1)
mat(ple4.biol) <- tmp

mat(ple4.biol)[mat(ple4.biol)==0.1] <- 0.2

mat(ple4.biol) <- replace(mat(ple4.biol), mat(ple4.biol)==0.2, 0.3)

@iagomosqueira iagomosqueira self-assigned this May 5, 2017
@iagomosqueira
Copy link
Member

These methods need redoing for classes like FLBiol, containing predictModel. Do tell us about other methods failing. Will do soon and get back to you.

@iagomosqueira
Copy link
Member

iagomosqueira commented Sep 11, 2017

Hi. Long delay, but after fixing initially window and trim I got to look at this.

First, you should not use @ and $ unless you can check you are passing to the object a value of the exact class required. To modify an FLQuant inside the mat slot of your FLBiol you can use three calls:

mat(ple4.biol) <- FLQuants(mat=tmp)
mat(ple4.biol) <- tmp
mat(ple4.biol, 'mat') <- tmp

First you can pass an FLQuants with tmp given the default name that matches the formula in @model. You can directly pass the new FLQuant, and it will be plcaed by default under the mat name in the FLQuants object. Or you could specify the element isnide the FLQuants object in the assigment operation.

The first two options have always been there, the third one I have just added, so it will be only available in gihutb until 2.6.5 is released next week.

Note that you cannot do

mat(ple4.biol)$mat <- tmp

as the $ subsetting is applied on the result of mat(ple4biol) which is already and FLQuant.

Do these options cover your needs?

Then, for accessing the contents of mat, we now have

# Returns the calculation
mat(ple4.biol)

# Returns the predictModel object
mat(ple4.biol, compute=FALSE)

# Same as above
mat(ple4.biol, FALSE)

# Returns the "mat" FLQuant inside the predictModel FLQuants
mat(ple4.biol, name="mat")

Again, please do let me know if this is eniught or we need to find better ways of using these slots

@dorleta @ssanchezAZTI

@iagomosqueira
Copy link
Member

Changes are available in the latest commit in master, 40273f4

@marchtaylor
Copy link
Author

Dear Iago - Thanks for this. This all works and makes sense to me now. Cheers, M

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants