From quique_3716 at hotmail.com Wed Nov 2 19:56:05 2016 From: quique_3716 at hotmail.com (quique3716) Date: Wed, 2 Nov 2016 11:56:05 -0700 (PDT) Subject: [datatable-help] How to produce variations of a text? Message-ID: <1478112965419-4726153.post@n4.nabble.com> # Given the text "I want cats", I'm trying to create the vector ("I see cats","I grab cats","I obtain cats") by creating a function that takes a vector with the words to change and applies to the initial text. However, I get the error "dim(X) must have a positive length". Here is where I got: desire<-"I want cats" actions<-c("see","grab","obtain") f<-function(x){ gsub("want", x, desire) } apply(actions,desire,f) # Could you please help me? Thanks in advance -- View this message in context: http://r.789695.n4.nabble.com/How-to-produce-variations-of-a-text-tp4726153.html Sent from the datatable-help mailing list archive at Nabble.com. From kpm.nachtmann at gmail.com Wed Nov 2 20:21:46 2016 From: kpm.nachtmann at gmail.com (nachti) Date: Wed, 2 Nov 2016 12:21:46 -0700 (PDT) Subject: [datatable-help] How to produce variations of a text? In-Reply-To: <1478112965419-4726153.post@n4.nabble.com> References: <1478112965419-4726153.post@n4.nabble.com> Message-ID: <1478114506197-4726154.post@n4.nabble.com> It has nothing to do with data.table, but what you need is probably this: lapply(actions, f) -- View this message in context: http://r.789695.n4.nabble.com/How-to-produce-variations-of-a-text-tp4726153p4726154.html Sent from the datatable-help mailing list archive at Nabble.com. From nfields at gmail.com Wed Nov 2 21:16:14 2016 From: nfields at gmail.com (affableambler) Date: Wed, 2 Nov 2016 13:16:14 -0700 (PDT) Subject: [datatable-help] How to produce variations of a text? In-Reply-To: <1478112965419-4726153.post@n4.nabble.com> References: <1478112965419-4726153.post@n4.nabble.com> Message-ID: <1478117774822-4726157.post@n4.nabble.com> Easy, just use sapply. desire<-"I want cats" actions<-c("see","grab","obtain") f<-function(x){ gsub("want", x, desire) } sapply(actions,FUN=f) see grab obtain "I see cats" "I grab cats" "I obtain cats" Alternatively, you could use a for loop, however this is much less efficient. vector<-c() for(action in actions){ vector<-c(vector,gsub("want",action,desire)) } print(vector) [1] "I see cats" "I grab cats" "I obtain cats" -- View this message in context: http://r.789695.n4.nabble.com/How-to-produce-variations-of-a-text-tp4726153p4726157.html Sent from the datatable-help mailing list archive at Nabble.com. From kpm.nachtmann at gmail.com Wed Nov 2 22:56:44 2016 From: kpm.nachtmann at gmail.com (nachti) Date: Wed, 2 Nov 2016 14:56:44 -0700 (PDT) Subject: [datatable-help] How to produce variations of a text? In-Reply-To: <1478117774822-4726157.post@n4.nabble.com> References: <1478112965419-4726153.post@n4.nabble.com> <1478117774822-4726157.post@n4.nabble.com> Message-ID: <1478123804572-4726160.post@n4.nabble.com> @affableambler Explicit loops (e.g. for) are inefficient and should not be used if it's possible. unlist(lapply(...)) is safer than sapply(...) I posted the lapply version, because if the result should be used within a data.table, the right handside (right from :=) should be a list. ~g -- View this message in context: http://r.789695.n4.nabble.com/How-to-produce-variations-of-a-text-tp4726153p4726160.html Sent from the datatable-help mailing list archive at Nabble.com. From nfields at gmail.com Wed Nov 2 23:31:12 2016 From: nfields at gmail.com (affableambler) Date: Wed, 2 Nov 2016 15:31:12 -0700 (PDT) Subject: [datatable-help] How to produce variations of a text? In-Reply-To: <1478123804572-4726160.post@n4.nabble.com> References: <1478112965419-4726153.post@n4.nabble.com> <1478117774822-4726157.post@n4.nabble.com> <1478123804572-4726160.post@n4.nabble.com> Message-ID: <1478125872270-4726161.post@n4.nabble.com> @nachti I wasn't familiar with unlist. Thanks! -- View this message in context: http://r.789695.n4.nabble.com/How-to-produce-variations-of-a-text-tp4726153p4726161.html Sent from the datatable-help mailing list archive at Nabble.com. From jgarrigan at gmail.com Fri Nov 4 01:32:11 2016 From: jgarrigan at gmail.com (pedropumpalot) Date: Thu, 3 Nov 2016 17:32:11 -0700 (PDT) Subject: [datatable-help] Using apply over two data frames with destPoint function Message-ID: <1478219531872-4726187.post@n4.nabble.com> Hi, How can I use apply function in conjunction with the destPoint function from the geosphere package across two data frames? I have tried the following but I am getting an error classEndPoints <- t(apply(DF1[,c(7:8,64:70)],DF2[,c(1:2)],1, function(x) destPoint(x[1:2],x[3:9],x[10:11]))) I get the following error: Error in match.fun(FUN) : '1' is not a function, character or symbol I have lat and long coordinates in DF1 in columns 7 & 8 and bearings (B1-B7) in columns 64 to 70 Lat Long ..... B1 B2 B3 B4 B5 B6 B7 -8.609117 52.69373 ..... 10 20 30 40 50 60 70 -8.607815 52.69444 ..... 80 90 100 110 120 130 140 In my other data frame DF2 I have 21 rows of data across 2 columns. Class From To Class 0 0 234 Class 1 234 468 Class 2 468 936 . . Class 20 1500 2500 I wish to use this data with the destPoint function which takes the form destPoint(p, b, d) where p is a vector or matrix of long and latitude, b is a single bearing and d is a distance. I would like to calculate the destPoint function for each row of data as follows: destPoint(-8.609117 52.69373,10, 0) destPoint(-8.609117 52.69373,20, 0) destPoint(-8.609117 52.69373,30, 0) . . . destPoint(-8.609117 52.69373,70, 0) destPoint(-8.609117 52.69373,10, 234) destPoint(-8.609117 52.69373,20, 234) . . destPoint(-8.609117 52.69373,70, 234) destPoint(-8.609117 52.69373,10, 234) destPoint(-8.609117 52.69373,20, 234) . . destPoint(-8.609117 52.69373,70, 234) destPoint(-8.609117 52.69373,10, 468) . . destPoint(-8.609117 52.69373,70, 468) destPoint(-8.609117 52.69373,10, 468) . . destPoint(-8.609117 52.69373,70, 468) destPoint(-8.609117 52.69373,10, 936) . . destPoint(-8.609117 52.69373,70, 936) Can anyone help me in the approach to this problem, please note I am still getting to grips with R so go gentle!! -- View this message in context: http://r.789695.n4.nabble.com/Using-apply-over-two-data-frames-with-destPoint-function-tp4726187.html Sent from the datatable-help mailing list archive at Nabble.com. From zkokia at 163.com Thu Nov 10 11:47:36 2016 From: zkokia at 163.com (zkokia) Date: Thu, 10 Nov 2016 02:47:36 -0800 (PST) Subject: [datatable-help] Problem with calling a function in RcppArmadillo to R? Message-ID: <1478774856276-4726408.post@n4.nabble.com> I want to call a kronecker function, the corresponding file in the fn_kron.hpp file are: 1) In "R": Rcpparma_hello_world <- function () { .Call ( 'fn_kron_hpp_rcpparma_hello_world', PACKAGE = 'fn_kron.hpp') }} Rcpparma_outerproduct <- function (x) { .Call ( 'fn_kron_hpp_rcpparma_outerproduct', PACKAGE = 'fn_kron.hpp', x) }} 2) "src": #Include Using namespace Rcpp; // rcpparma_hello_world Arma :: mat rcpparma_hello_world (); RcppExport SEXP fn_kron_hpp_rcpparma_hello_world () { BEGIN_RCPP Rcpp :: RObject rcpp_result_gen; Rcpp :: RNGScope rcpp_rngScope_gen; Rcpp_result_gen = Rcpp :: wrap (rcpparma_hello_world ()); Return rcpp_result_gen; END_RCPP }} // rcpparma_outerproduct Arma :: mat rcpparma_outerproduct (const arma :: colvec & x); RcppExport SEXP fn_kron_hpp_rcpparma_outerproduct (SEXP xSEXP) { BEGIN_RCPP Rcpp :: RObject rcpp_result_gen; Rcpp :: RNGScope rcpp_rngScope_gen; Rcpp :: traits :: input_parameter :: type x (xSEXP); Rcpp_result_gen = Rcpp :: wrap (rcpparma_outerproduct (x)); Return rcpp_result_gen; END_RCPP }} 3)"man" includes rcpparma_hello_world.Rd and fn_kron.hpp-package.Rd two documents. # ================================================= So now the question is that I not know how to call the function in R ? Library (RcppArmadillo) Ws <- .Call ( 'fn_kron_hpp_rcpparma_outerproduct', PACKAGE = 'fn_kron.hpp', listw) Note: I used the R kronecker function, the code is as follows: I_T <- Diagonal (T) Ws <- kronecker (I_T, listw) I do not understand what 'x' represents in . Call ( 'fn_kron_hpp_rcpparma_outerproduct', PACKAGE = 'fn_kron.hpp', x). -- View this message in context: http://r.789695.n4.nabble.com/Problem-with-calling-a-function-in-RcppArmadillo-to-R-tp4726408.html Sent from the datatable-help mailing list archive at Nabble.com. From Eric.Archer at noaa.gov Wed Nov 23 19:02:41 2016 From: Eric.Archer at noaa.gov (Eric Archer) Date: Wed, 23 Nov 2016 10:02:41 -0800 (PST) Subject: [datatable-help] Create matrix of columns from grouped rows Message-ID: <1479924161112-4726777.post@n4.nabble.com> I am trying to find the most efficient (fastest) way of manipulating a data.table object that contains genetic data. The format is in the following toy example, where rows represent alleles for individuals and columns are separate loci. Each individual will be represented in one or more rows depending on what the ploidy of the loci are. In this example, there are three tetraploid loci (4 alleles per locus) genotyped for three individuals (1:3). In my real data, all loci will always have the same ploidy. > library(data.table) > dt <- data.table( + id = as.character(rep(1:3, each = 4)), + loc1 = factor(sample(c("C", "T"), 12, rep = T)), + loc2 = factor(sample(c("C", "T"), 12, rep = T)), + loc3 = factor(sample(c("C", "T"), 12, rep = T)), + key = "id" + ) > dt id loc1 loc2 loc3 1: 1 T T T 2: 1 C T C 3: 1 T C T 4: 1 C C T 5: 2 T C C 6: 2 T T T 7: 2 C T T 8: 2 C T C 9: 3 C T T 10: 3 T T T 11: 3 T C T 12: 3 T T C What I'm looking for is the fastest way to convert this data.table to a matrix where each row has the entire genotypes for one individual with the alleles for a locus in sequential columns. The code I currently have for this follows. > ids <- dt[, unique(id)] > .cbindColFunc <- function(x) { + do.call(cbind, as.list(as.character(x))) + } > mat <- do.call(rbind, lapply(ids, function(i) { + dt[i, do.call(cbind, lapply(.SD, .cbindColFunc)), .SDcols = !"id"] + })) > num.alleles <- ncol(mat) / (ncol(dt) - 1) > colnames(mat) <- paste(rep(colnames(dt)[-1], each = num.alleles), > 1:num.alleles, sep = ".") > mat <- cbind(id = ids, mat) > mat id loc1.1 loc1.2 loc1.3 loc1.4 loc2.1 loc2.2 loc2.3 loc2.4 loc3.1 loc3.2 loc3.3 loc3.4 [1,] "1" "T" "C" "T" "C" "T" "T" "C" "C" "T" "C" "T" "T" [2,] "2" "T" "T" "C" "C" "C" "T" "T" "T" "C" "T" "T" "C" [3,] "3" "C" "T" "T" "T" "T" "T" "C" "T" "T" "T" "T" "C" Is there a faster, more data.table friendly way to do it? Thanks in advance! Eric -- View this message in context: http://r.789695.n4.nabble.com/Create-matrix-of-columns-from-grouped-rows-tp4726777.html Sent from the datatable-help mailing list archive at Nabble.com. From kpm.nachtmann at gmail.com Wed Nov 23 20:11:30 2016 From: kpm.nachtmann at gmail.com (nachti) Date: Wed, 23 Nov 2016 11:11:30 -0800 (PST) Subject: [datatable-help] Create matrix of columns from grouped rows In-Reply-To: <1479924161112-4726777.post@n4.nabble.com> References: <1479924161112-4726777.post@n4.nabble.com> Message-ID: <1479928290781-4726778.post@n4.nabble.com> Try this: t1 <- dt[, unlist(.SD), by = id] t(unstack(t1, form = V1 ~ id)) (I think you get done with the colnames yourself ...) Cheers, ~g -- View this message in context: http://r.789695.n4.nabble.com/Create-matrix-of-columns-from-grouped-rows-tp4726777p4726778.html Sent from the datatable-help mailing list archive at Nabble.com. From Eric.Archer at noaa.gov Wed Nov 23 20:20:22 2016 From: Eric.Archer at noaa.gov (Eric Archer) Date: Wed, 23 Nov 2016 11:20:22 -0800 (PST) Subject: [datatable-help] Create matrix of columns from grouped rows In-Reply-To: <1479928290781-4726778.post@n4.nabble.com> References: <1479924161112-4726777.post@n4.nabble.com> <1479928290781-4726778.post@n4.nabble.com> Message-ID: Thanks! That's about 2-3x faster. I wasn't familiar with stack/unstack. Now I have another tool. Cheers, e. ---- *Eric Archer, Ph.D.* Southwest Fisheries Science Center (NMFS/NOAA) 8901 La Jolla Shores Drive La Jolla, CA 92037 USA 858-546-7121 (work) 858-546-7003 (FAX) Marine Mammal Genetics Group: swfsc.noaa.gov/mmtd-mmgenetics GitHub: github/ericarcher & Adjunct Professor, Marine Biology Scripps Institution of Oceanography University of California, San Diego http://profiles.ucsd.edu/frederick.archer " *The universe doesn't care what you believe. The wonderful thing about science is that it doesn't ask for your faith, it just asks for your eyes.*" - Randall Munroe "*Lighthouses are more helpful than churches.*" - Benjamin Franklin "*...but I'll take a GPS over either one.*" - John C. "Craig" George On Wed, Nov 23, 2016 at 11:11 AM, nachti [via R] < ml-node+s789695n4726778h3 at n4.nabble.com> wrote: > Try this: > > t1 <- dt[, unlist(.SD), by = id] > t(unstack(t1, form = V1 ~ id)) > > (I think you get done with the colnames yourself ...) > > Cheers, > ~g > > ------------------------------ > If you reply to this email, your message will be added to the discussion > below: > http://r.789695.n4.nabble.com/Create-matrix-of-columns-from-grouped-rows- > tp4726777p4726778.html > To unsubscribe from Create matrix of columns from grouped rows, click here > > . > NAML > > -- View this message in context: http://r.789695.n4.nabble.com/Create-matrix-of-columns-from-grouped-rows-tp4726777p4726779.html Sent from the datatable-help mailing list archive at Nabble.com. -------------- next part -------------- An HTML attachment was scrubbed... URL: From amin.f.saad at gmail.com Thu Nov 24 01:06:04 2016 From: amin.f.saad at gmail.com (Memo712) Date: Wed, 23 Nov 2016 16:06:04 -0800 (PST) Subject: [datatable-help] Replacing NA values for a categorical variable results in numerical replacements Message-ID: <1479945964277-4726789.post@n4.nabble.com> Dear forum members, I am new to R and I have downloaded the car library and I am working with the Prestige database. In this database, I am trying to replace replace NA value for a Factor variable called type and I used the following code. Prestige.bc <- Prestige %>% mutate(type = ifelse(is.na(type),"bc",type)) I was successful in doing that but the other values, which were prof, bc, wc, changed to numbers 2. 3 and 1 and only NA were replaced by bc. For example: type 2 2 3 1 bc 1 3 3 2 I really appreciate any help. -- View this message in context: http://r.789695.n4.nabble.com/Replacing-NA-values-for-a-categorical-variable-results-in-numerical-replacements-tp4726789.html Sent from the datatable-help mailing list archive at Nabble.com. From nfields at gmail.com Thu Nov 24 03:23:18 2016 From: nfields at gmail.com (affableambler) Date: Wed, 23 Nov 2016 18:23:18 -0800 (PST) Subject: [datatable-help] Replacing NA values for a categorical variable results in numerical replacements In-Reply-To: <1479945964277-4726789.post@n4.nabble.com> References: <1479945964277-4726789.post@n4.nabble.com> Message-ID: <1479954198675-4726790.post@n4.nabble.com> This may not be the most elegant solution, but one way to do it would be to convert the factor to a character vector, replace the NAs, and then convert it back to a factor: Prestige.bc<-Prestige Prestige.bc$type<-as.character(Prestige.bc$type) Prestige.bc$type[which(is.na(Prestige.bc$type))]<-'bc' Prestige.bc$type<-as.factor(Prestige.bc$type) -- View this message in context: http://r.789695.n4.nabble.com/Replacing-NA-values-for-a-categorical-variable-results-in-numerical-replacements-tp4726789p4726790.html Sent from the datatable-help mailing list archive at Nabble.com.