Quantcast
Channel: User Nathan Werth - Stack Overflow
Browsing latest articles
Browse All 41 View Live

Comment by Nathan Werth on How to conditionally process sections in Rmarkdown

Have you tried using knitr::is_html_output() to wrap the code in an if expression? I'm having trouble getting PDF books to compile, so I can't be sure right now.

View Article



Comment by Nathan Werth on Duplicates in data: how to keep a specific row of...

For getting user input in the middle of a script, you could use the readline() function.

View Article

Comment by Nathan Werth on Duplicates in data: how to keep a specific row of...

@Emman You can record your reasons for picking rows in comments at the top of the data file with the keep_this_one column. read.table and its children have an option to ignore commented lines. Just...

View Article

Comment by Nathan Werth on Create and Update an element of List within a For...

What is PARAMS? Your code doesn't show it being created before you assign to a subset of it.

View Article

Comment by Nathan Werth on function for calculating sets intersections for...

What's being counted for the last row with only FALSE?

View Article


Comment by Nathan Werth on what is the behavior of install.packages in case...

Because you didn't specify in install.packages that you wanted a source package, R also looked for binary packages. Probably in case the package you want were available as a binary but not as source.

View Article

Comment by Nathan Werth on what is the behavior of install.packages in case...

Does zizzio have an entry in the PACKAGES file? If so, does its Depends: entry require a version of R above 3.5.3?

View Article

Comment by Nathan Werth on How do I get my script to cycle through all...

You could do lapply(3:31, combn, x = 1:31), then loop over that. Note that there are 300,540,195 combinations of 15 elements from 31, so it might take a while.

View Article


Comment by Nathan Werth on R: Moving up and down the callstack (frames)

recover() while debugging does what you want. Repeated calls will add more frames to the list of choices (they look like eval(substitute(browser...), but the original frames should be at the top of the...

View Article


Comment by Nathan Werth on Creating a chore wheel for a household

Would adding a "no chore" option, which can have multiple people assigned to it in a week, make this possible?

View Article

Comment by Nathan Werth on Make a tree structure from a path name

Do you know that all inputs will have the same "depth?" All your examples are 3 deep, but is it possible you'd need to process something like "A/B"? What would the result, specifically dictionary["A"]...

View Article

Comment by Nathan Werth on data.table::setorder on data.frame input order...

TLDR: Call setDT(x0) at the top level. Don't expect setDT to "bubble up" through environments. R as a language does not have reference semantics. setDT(x) reassigns a new object to the name "x". The...

View Article

Comment by Nathan Werth on Difficulty reading .tab file in R due to newline...

You can inspect the actual character values in R with readChar("data.tab", 300). Replace the 300 with whatever number gets you to the third line. Then you'll see where the newline and tab characters...

View Article


Answer by Nathan Werth for How do I change the name of a variable in a fitted...

@AndrewGustar is right: your way can be done by replacing every instance of the variable name throughout the list. But those names appear in a lot of places, as both character vectors and language...

View Article

Answer by Nathan Werth for Python create an iterator/generator with feedback

It's possible but confusing. If you want to keep the sequence of x values and the calculations on x separate, you should do this explicitly by not involving x with an iterator.def...

View Article


Answer by Nathan Werth for Merging two lists of elements into one with the...

You can get a simple "merge" with a lapply loop:all_names <- union(names(DiffINT), names(DiffEXT))DiffsMerged <- lapply( X = all_names, FUN = function(name) { list(DiffINT[[name]],...

View Article

Answer by Nathan Werth for bind_rows to each group of tibble

split from base R will divide a data frame into a list of subsets based on an index.b %>% split(b[["id"]]) %>% lapply(bind_rows, a) %>% lapply(select, -"id") %>% bind_rows(.id = "id")# # A...

View Article


Answer by Nathan Werth for Revert data.frame back to vectors in R

A data.frame is already a list of vectors (of equal length). You can treat it just like a list.class(mtcars)# [1] "data.frame"is.list(mtcars)# [1] TRUEmtcars[["cyl"]]# [1] 6 6 4 6 8 6 8 4 4 6 6 8 8 8 8...

View Article

Answer by Nathan Werth for Error in R data.table fwrite()?

fwrite uses a function written in C to do all the work, with custom handlers for dates and datetimes used by default. I suggest creating an issue on the package's GitHub page.Until then, this...

View Article

Answer by Nathan Werth for r - source script file that contains unicode...

Use source without specifying the encoding, and then modify the vector's encoding with Encoding:source(script)letters_fa# [1] "الÙ\u0081""ب""Ù¾""ت""Ø«"# [6] "ج""Ú†""Ø­""Ø®""ر"# [11]...

View Article

Answer by Nathan Werth for Split facet plot into list of plots

This is similar to Moody_Muddskipper's answer, but works with any type of faceting (facet_grid or facet_wrap), handles arbitrary expressions in facets, and doesn't draw facet strip...

View Article


Answer by Nathan Werth for Is there any way to not to loop this

I'm going to address your problem with a simple vector instead of a data.frame. It's easier to reason about, and will be easily adapted to a loop over a data.frame's columns.set.seed(2)v <-...

View Article


Answer by Nathan Werth for How to set a max limit to the characters of the...

You can adjust the labels with scale_x_discrete, which means no editing of the dataset is done.g+geom_bar(aes(fill = drv), position = position_stack(reverse = TRUE)) + scale_x_discrete( labels =...

View Article

Answer by Nathan Werth for How to change display format of POSIXct in R...

You can replace the print method for the class. Looking at the code for print.POSIXct:function (x, tz = "", usetz = TRUE, ...) { max.print <- getOption("max.print", 9999L) FORM <- if...

View Article

Answer by Nathan Werth for write.table() results in permission denied -...

The path you give to write.table should name a file. You have it naming a directory. R won't replace that directory with a file (thank goodness).Try this:write.table(dataframe,...

View Article


Answer by Nathan Werth for Calculating distance between 2 elements of a data...

This is the perfect case for findInterval(). We'll create a vector of the breaks between categories and use those to calculate scaling factors.size_breaks <- c(size_df[["size_min"]],...

View Article

Answer by Nathan Werth for subsetting a data.table based on a named list

You can use the CJ (Cross Join) function from data.table to make a filtering table from the list.lookup <- do.call(CJ, param)head(lookup)# a b c# 1: 1 2 5# 2: 1 2 7# 3: 1 2 10# 4: 1 3 5# 5: 1 3 7#...

View Article

Answer by Nathan Werth for How to efficiently replace one set of values with...

Combine the old and new values in another data.table and do a "lookup assignment".replacements <- data.table( old = c("12", "11", "14", "15"), new = c("11", "12", "15", "16"))dt[ replacements, on =...

View Article

Answer by Nathan Werth for Efficiently merging large data.tables

Keyed assignment should save memory.dt1[dt2, on = "id", x5 := x5]Should we use a DB library to get this done?That's probably a good idea. If setting up and using a database is painful for you, try the...

View Article



Answer by Nathan Werth for Distinguish between an empty list or an empty data...

It's often easier to clean the data as soon as you get it from the API. Then everything that follows can rely on safe assumptions.For this example, create a function that returns a consistently...

View Article

Answer by Nathan Werth for How to predict after lm.wfit with new data?

lm is slower than lm.fit and lm.wfit because it uses those functions internally. You've pulled some other internal work out by creating the model matrix outside the benchmark. That's fine if you expect...

View Article

Answer by Nathan Werth for Inspect if an argument was passed positionally or...

You can use a wrapper to add an extra step between the user and the function. In that step, you can examine the arguments before the names matter. Note that this depends on the fact that b doesn't have...

View Article

Answer by Nathan Werth for What is the equivalent of python's idxmin() in R?

You can use match:## Create sorted data.framed <- data.frame( candy_type = c('A','A','B','B','C','C','C'), sequence = c(2,1,1,2,2,3,1), color =...

View Article


Answer by Nathan Werth for Note from win-builder (Building a R package)

You exclude CODE_OF_CONDUCT.md from the built package in your .Rbuildignore file. So the README.md in the built package can't find the linked file.

View Article

Answer by Nathan Werth for Change data types using a list of data type names

We can use mapply to feed utils::as pairs of columns and types. This won't work for the factor columns, so those are handled separately.fcols <- my_types == "factor"my_df[!fcols] <- mapply(as,...

View Article

Answer by Nathan Werth for how to clean a query string in a URL?

The utils package, which comes with R, has a URLdecode function:URLdecode("snack%20over%20flow")# [1] "snack over flow"It isn't vectorized (can only handle one string at a time), but that's easy to...

View Article


Answer by Nathan Werth for Finding the overlap between two data frames in R,...

The data.table package is great for fast merging of tables. It also comes with a vectorized between function for just this type of task.library(data.table)# Convert the data.frames to...

View Article


Answer by Nathan Werth for How do I create multiple subsets?

Use the split function to create a list of subsets based on a column. The list will have the "splitting" groups as names.Using the PlantGrowth dataset as an example:summary(PlantGrowth)# weight group #...

View Article

Answer by Nathan Werth for How can I source a R file and store all variables...

You can use the local parameter for source to save the results in an environment. Here's a function that wraps it up and converts the environment into a list:source_in_list <- function(path) { e...

View Article

Answer by Nathan Werth for R give column names when creating dataframe from...

To answer your question exactly, use setNames:m %>% data.frame() %>% setNames(col_names)However, this is a "one-liner" in the same way nested function calls are one-liners.

View Article

Answer by Nathan Werth for Assigning names to certain ranges of values in...

In R, multiline code is not necessarily inefficient. Code is often optimized if code works with one subset of a vector at a time.Here's the very plain way of doing what you want:front <-...

View Article

Browsing latest articles
Browse All 41 View Live




Latest Images