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

Deletion of columns deletes more columns than expected #24

Closed
MartinKies opened this issue Jun 10, 2020 · 3 comments
Closed

Deletion of columns deletes more columns than expected #24

MartinKies opened this issue Jun 10, 2020 · 3 comments

Comments

@MartinKies
Copy link
Contributor

Hello, its me again. I found pretty strange behavior regarding the deletion of columns of a data frame (and possibly a matrix, not tested). Please find a MWE attached.

MinDelExample_sol.zip
DeletionBehavior

@skranz
Copy link
Owner

skranz commented Jun 10, 2020

Hmm, I don't see the problem in your screenshot when you just show example_df[,-2]. In the output the 5 remaining columns seem to be shown.

The command

example_df <- example_df[,2]

indeed won't work with RTutor. Using the same variable on the left and right in an assignment is often problematic in RTutor. More precisely, problems arise if calling the command twice would yield different results than calling it only once. This restriction is hard to fix in RTutor. So in particular deleting columns or rows won't work. If you want to do this you have to set a new variable name on the LHS.

@MartinKies
Copy link
Contributor Author

Yes you are right. Maybe something I will have a look at down the line. Right now I was able to circumvent the problem as per your suggestion.

And you are right that just showing example_df[,-2] did indeed deliver the desired output. I confused myself there.

@skranz
Copy link
Owner

skranz commented Jun 17, 2020

Just as a remark. Unrelated to the limitations of RTutor I also try to write my personal R code (at least the one outside packages) such that I can run a line twice without changing the result. E.g. if I want to remove a column, I like most to use a function like:

remove.cols = function(dat, cols) {
  dat[,setdiff(colnames(dat), cols)]
}

which returns the same data frame even when called errorneously twice. (I don't like e.g. that select(dat, -mycol) would throw an error if mycol does not exist anymore in dat). The thing is that I most often work interactively with R and it often happens that I run only some but not all data preparation steps twice and it is nice if this does not yield by accident wrong results. In this sense, RTutor forces you to follow this convention.

@skranz skranz closed this as completed Jun 17, 2020
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