I don't quite get what you want. Can you provide a brief sample input and desired output?
I have founf several close but not quite answers to this problem that I know is probably very simple.
I want to take a list of equal length vectors (could be numeric or character) and paste them together using "." as the separator. the number of columns being passed is unknown.
Here are my attempts:
Thank you in advance!Code:x <- CO2[, 1:3] y <- CO2[, 1:4] paste(x, sep=".") paste(y, sep=".") paste2 <- function(x) paste(x, sep=".") #attempt with do.call do.call("paste2", y)
"If you torture the data long enough it will eventually confess."
-Ronald Harry Coase -
I don't quite get what you want. Can you provide a brief sample input and desired output?
"His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich
The x and the y were just showung the number of columns was unknown.
I want the outcome for x to be:
and for y the output would have the same plus another dot and the 4th column. Note the length of the new vector will be = to length of the combined vectors.Code:"Qn1.Quebec.nonchilled" "Qn1.Quebec.nonchilled" "Qn1.Quebec.nonchilled" ...
Maybe this is an mapply solution but I'm in the car now without R so can't check.
"If you torture the data long enough it will eventually confess."
-Ronald Harry Coase -
is this what you mean.Code:dat<-data.frame(x=rnorm(10), y=letters[1:10]) cat(as.character(dat$x),sep=".", "\n") cat(as.character(dat$y),sep=".", "\n")
OR
Code:f1<-function(x) cat(as.character(x),sep=".", "\n") f2<- colwise(f1) f2(dat)
Last edited by Lazar; 03-12-2012 at 05:56 PM. Reason: forgot new line command
@Lazar I can't check this yet but the use of cat doesn't't seem right. I want to combine predictors (row by row) to make a final vector that is a character string of the predictors pasted together.
This may also be q job for reduce.
I would have thought I could have just used paste though.
One more problem is that if any calls in the paste are NA then it needs to return NA.
"If you torture the data long enough it will eventually confess."
-Ronald Harry Coase -
Do either of these do what you want?
Code:x <- CO2[, 1:3] y <- CO2[, 1:4] apply(x, 1, paste, collapse = ".") apply(x, 1, function(x){if(any(is.na(x))){NA}else{paste(x, collapse = ".")}})
"His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich
trinker (03-12-2012)
@Dason I was just about to write that I got the apply and was going to work on NA. when apply came to me I was like "You bone head". That saves me from figuring out the NA problem. Much appreciated.
"If you torture the data long enough it will eventually confess."
-Ronald Harry Coase -
No problem. Sorry about the horribly formatted code. When I write code here and don't actually check it within R I tend to just write gross one-liners.
"His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich
Just looking at the OP, you say you want to use a list of vectors but then refer to columns. Lists don't have columns! You don't provide an example of the input or the output! Bad form!
I was doing it from my phone. Geesh cut a guy a break BGAnd technically a data frame is a list of equal length vectors. So CO2 is an example of the input. I took care of the output when dason asked. There lies your criticisms in a pile of rubbish. I hypothesize this post is just a red herring and you're playing both the bots and raptors.
Is your Monday that hard that you're picking on poor trinker now? And besides my teeth are 4 inches long. I ask is it wise to pick fights with raptors. That makes me wonder why would you not be afraid? Unless....That's it!!!!!!!
We're all worried about bots and raptors meanwhile who's looking out for Wepwawet the one who ushers the dead into hell. My gosh bots and raptors may take your life but Wepwawet takes your soul. Beware TSers he's a snake in the grass.
Last edited by trinker; 03-13-2012 at 06:11 AM. Reason: I can't spell soul
"If you torture the data long enough it will eventually confess."
-Ronald Harry Coase -
Tall tales.
Btw, you could have did the paste thing quite easily from do.call if you understand that you need to supply it a list object that contains all the parameters you want to use. This includes the "sep" parameter.
The reason your paste2 fails is because you don't pass anything else that paste requires ... like the stuff to paste! You'd want to do something likeCode:set.seed(100) df <- data.frame(A = runif(10, 0, 10), B = runif(10, 0, 10), C = runif(10, 0, 10)) y <- as.list(df) # make it a list y$sep = "-Wepwawet-" # set our separator do.call("paste", y) # [1] "3.07766110869125-Wepwawet-6.24996477039531-Wepwawet-5.35811153938994" # [2] "2.57672501029447-Wepwawet-8.82165518123657-Wepwawet-7.10803845431656" # [3] "5.52322433330119-Wepwawet-2.8035383997485-Wepwawet-5.38348698290065" # [4] "0.563831503968686-Wepwawet-3.98487901547924-Wepwawet-7.4897222686559" # [5] "4.68549283919856-Wepwawet-7.6255108229816-Wepwawet-4.20101450523362" # [6] "4.83770735096186-Wepwawet-6.69021712383255-Wepwawet-1.71420212602243" # [7] "8.12402617651969-Wepwawet-2.04612161964178-Wepwawet-7.70301609765738" # [8] "3.70320537127554-Wepwawet-3.57524853432551-Wepwawet-8.81953587755561" # [9] "5.46558595029637-Wepwawet-3.59475114848465-Wepwawet-5.49096710281447" # [10] "1.70262051047757-Wepwawet-6.90290528349578-Wepwawet-2.77723756618798"
Code:mypaste <- function(..., sep = ".") {paste(..., sep = sep)}
trinker (03-13-2012)
Very helpful and informative. Why didn't my paste2 work then? This helps me understand do.call a bit better. I only started using it recently when you explained it about a month back.
"If you torture the data long enough it will eventually confess."
-Ronald Harry Coase -
But really... this is a situation that is probably more appropriate for apply.
do.call is nice and can help in certain situations but for readability purposes I would opt for the apply method in this case.
"His programming is malfunctioning. It begins! Get your weapons, he's going to become a killbot!!!" - bryangoodrich
Didn't you read the bottom of my comment?! Compare my wrapper to yours.
@Dason, I agree that the semantics for using apply are better, but this approach could offer speed boosts, and it is good that Trinker understands how to utilize it. For if it is used inside a function that needs to be fast, then its obscure approach may still be the better choice, because functions in one's package don't necessarily need to be the most comprehensible code; they need to be efficient!
Oh!!!!!!!!!!! I get it. the sep=sep sets up "hey I'm going to send you something called sep" and the y$sep is "that thing I told you I was going to send you. It was part of the list I'm giving you do.call because you only seem to like list stuff" (do.call is a list snob). Thanks bryangoodrich. Even clearer. You're still a Wepwawet.
"If you torture the data long enough it will eventually confess."
-Ronald Harry Coase -
|
|