function and tibble doesn't preserve the changes

I wrote a small function to change row values over some columns.
I mea, I just copy some column values from rows x to rows y. And I declared "v" as a condition in order to change a value to 1.

edic_c <- function(x,y,v) {

if (v==1) {

But when I tried to applied, in some rows, I just simply didn't preserve the change I declared.
I don't know if tha't because I am mixing a data frame nd a tibble object or anything related.
Here I am listing the rows 1589 and 1590. They should appear with the values from 125, but It doesn't.

daaz>%filter(orden %in% c(125,1589,1590))
  flip src     num1 num2    lastN
  <chr> <chr>    <dbl> <chr>    
1 NA    14 - …  1.30e7 9        ALE
2 NA    17 - … NA      NA       ALE
3 NA    17 - … NA      NA       ALE

If I manueally declare this, It works...


Can you guide me, please?
Thanks for your time and interest.

I tried using dplyr and maggrite the same goal. I can see the changes when I run a line directly. but the function doesn't store the changes. I have no idea. I used %<>% on magritte. I even declared using dplyr dataz=dataz%>%... but the output is always the same...


Having a quick read I think this is what is currently wrong from the code you have. Your function call (edic_c ) should have its output assigned to an object:


df_output <- edic_c(125,1589,0)

Secondly, change your function edic_c to have a fourth argument which is the dataframe as it looks like you have both daaz and dataz as dataframes that contain information. If you do these two changes it should get you there.

1 Like

This is due to namespaces.
When you are in a function and assigning values, you do so in that functions namespace, rather than in the global environment.
To illustrate:

(hiris <- head(iris))

hirischange <- function(x){
  hiris[,"Sepal.Length"] <- x



the global assignment operator is <<- if you must use it.(be careful though !)

1 Like

It didn't work. I edited as you suggested.

I think you were right, nirgrahamuk.
Using <<- declaring the function seems that was the issue.
I was using = or <-, so the function worked using directly the console, but not as a function.

This topic was automatically closed 7 days after the last reply. New replies are no longer allowed.

If you have a query related to it or one of the replies, start a new topic and refer back with a link.