trinker (10-14-2012)
I think you meant to put dput2 and not repex.
And this isn't working for me!
Of course I'm just being facetious but I had to find a way to break your functionCode:dput2(mtcars + 2)
I don't have emotions and sometimes that makes me very sad.
trinker (10-14-2012)
Yeah dason it was supposed to be dput2, originally it was repex for reproducible example but I couldn't remember the name for the function. And yes I knew it was fairly simple to break as I used:
but generally I want to dput a dataframe or the head of a dataframe. Anything else it works but you'll have to rename the object it's being assigned to. I don't really have any work around for all the different scenerios that could break it but if anyone has an easy solution I'm all eyes.Code:dput2(head(head(mtcars, 20)))
"If you torture the data long enough it will eventually confess."
-Ronald Harry Coase -
I doubt there is a direct solution. Consider
How should it know which variable you want the name to be? You aren't dputting either object directly so there is no clear way to tell.Code:val <- 10 dput2(head(mtcars, val))
I don't have emotions and sometimes that makes me very sad.
an alternative would be to just use dat as the object we assign to since you're likely making a reproducible example anyway. What are your thoughts on that?
"If you torture the data long enough it will eventually confess."
-Ronald Harry Coase -
No I think the way you're doing it is fine. In 99% of the cases you'll correctly identify the name.
I don't have emotions and sometimes that makes me very sad.
Thought I'd share this one cause it's simple and has the possibility for lots of applications. outer is pretty nice but it doesn't take vectors. I once asked at SO how to make outer work with vectors and got two responses (LINK). I recieved two great responses but have stuck with the Vectorize solution though it's slower because it is more readable to me. Anyway this v.outer is a vectorized version of outer that you can supply a function to and that functions arguments. Acts on a matrix or data.frame.
Code:v.outer <- function(x, FUN, digits = 3, ...){ FUN <- match.fun(FUN) if (is.matrix(x)) { x <- as.data.frame(x) } if (is.list(x) & !is.data.frame(x)){ if (is.null(names(x))) { names(x) <- paste0("X", seq_along(x)) } nms <- names(x) } else { nms <- colnames(x) } z <- outer( nms, nms, Vectorize(function(i,j) FUN(unlist(x[[i]]), unlist(x[[j]]), ...)) ) dimnames(z) <- list(nms, nms) if (is.numeric(z)) { z <- round(z, digits = digits) } z } v.outer(mtcars, cor) v.outer(mtcars, cor, method="kendall")
"If you torture the data long enough it will eventually confess."
-Ronald Harry Coase -
Lazar (10-30-2012)
ps I realize cor is a lttle silly in that it already gives you this output but it was the first function that came to mind that takes an x and y vector and so I used it. here's an example with a function for pooled sd (again this is a bit silly in that you'd do pooled for all and the function for pooled may be incorrect) but it can help people understand more:
PS can anyone think of functions that take an x and y vectors and return a single value?Code:pooled.sd <- function(x, y) { n1 <- length(x) n2 <- length(y) s1 <- sd(x) s2 <- sd(y) sqrt(((n1-1)*s1 + (n2-1)*s2)/((n1-1) + (n2-1))) } v.outer(mtcars, pooled.sd)
"If you torture the data long enough it will eventually confess."
-Ronald Harry Coase -
um how about Euclidean distance:
Code:> euc.dist <- function(x,y) sqrt(sum((x - y) ^ 2)) > v.outer(mtcars, euc.dist)
trinker (10-30-2012)
I'm new to R, but I'm learning fast... With \Huge{ thanks! } to everyone adding to this thread.. Just finished going through all the code.. Simply amazing stuff. I couldn't get everything to work: The unwrap() function was especially frustrating. I tried for a couple hours to no avail; I did get wrap() to work fairly quickly with some substitutions to make it functional on my mac. I realize now the functions I was writing were very raw. But here they are:
1) a function that renames the column names to latex math format for my lab reports - yes I'm an undergrad
2) a search function that returns the first occurrences of unique values in a data.frame column
Code:################################################# # 1) # puts $dollar signs in front and behind all column names col_{sub} -> $col_{sub}$ # ################################################### amscols <- function(x){ colnames(x) <- paste( "$" , colnames(x) , "$" , sep = "" ) x } ################################################# # 2) # Returns a data.frame of the first occurances of all unique values of the "search" column # ############################################### getfirsts <- function(data, searchcol){ # Receives a data.frame and a "search" column # Returns a data.frame of the first occurances of all unique values of the "search" column rows <- as.data.frame(match(unique(data[[searchcol]]), data[[searchcol]])) firsts = data[rows[[1]],] return(firsts) }
Does using xtable from the xtable package not work for you to transfer tables to latex code?
I don't have emotions and sometimes that makes me very sad.
Thats what I use, but for the column names, I need subscripts and superscripts and the occasional equation which is translated by latex.
Code:library(xtable) #some code manipulating data into the format i like amscols(my.data.frame) # <- formats column names from colname to $colname$ mytable <-xtable( my.data.frame, caption="\\\\ \\textit{This is an example table with good latex formatting}", label="tab:mytable", align="cccccccccc", digits=2 ) print.xtable( mytable, type="latex", file="filepath.tex", include.rownames=F, table.placement="H", size="small" , caption.placement="top", sanitize.colnames.function=function(x){x} # <- lifesaver )
Hey Nathan,
Sorry about the unwrap causing frustration. I wrote it early in my understanding of R and didn't consider other platforms. Most likely the adaptations you did to wrap should be generalizable to unwrap. I must admit I no longer use these functions and so didn't really develop them any further. Glad to have you contributing to the discussions at talkstats.
"If you torture the data long enough it will eventually confess."
-Ronald Harry Coase -
Nathan G (12-18-2012)
No apology needed. I like the challenge and it's good me for me to go back and decode.. I started using R dealing exclusively with data.frames with the plyr and reshape2 packages and without any clear understanding of how they worked. Unraveling other peoples code helps me learn different methods and forces me to go back to the basics and dig into the nuances of R.
“Super genius… I like the way that rolls out! Super Genius!" - Wile E. Coyote, SG
"87.3% of all statistics are made up, the other 12.7% get done up.” - NRG
Extremely simple function that strips NAs from vectors. Has improved my efficiency a lot when working interactively with data.
Code:noNA <- function(input) { output <- input[!is.na(input)] return(output) }
Last edited by derksheng; 01-08-2013 at 08:44 AM.
Tweet |