Re: [R-es] DUDA LLENAR MATRIZ CREADA
David, No hay mucho detalle, pero aqui van algunas ideas: 1. Construye una funcion que tome como argumentos los parametros con los que generas tus muestras y construye una lista con los resultados. Cada componente de la lista contendra las *B *muestras sobre las cuales vas a realizar los analisis. 2. Utiliza lapply() para tomar cada entrada de la lista creada en 1. y aplica las pruebas que necesitas. Esta funcion debe tomar como argumentos (posiblemente) un data.frame() y realizar las pruebas. Por supuesto dentro de esa funcion solo extraerias los estadisticos de prueba y los valores p asociados a t.test(), wilcox.test, etc... 3. Usa do.call(rbind, L) para organizar los resultados. 4. Usa write.table() para exportarlos. Espero sea de ayuda. Saludos, Jorge.- 2015-02-26 6:14 GMT+11:00 David Contreras davidcontrera...@gmail.com: Buena tarde, Estoy llevando a cabo un trabajo y no encuentro la forma de llenar una matriz con el p_value y un estad�stico calculado. Un poco mas detallado, tengo muestras aleatorias, calculo por ejemplo la prueba t, wilcoxon, etc y requiero llevar por una parte los p_value de k muestras a una matriz creada anteriormente y por otro lado el valor del estad�stico de las mismas k muestras a otra matriz creada anteriormente. Las k muetras estar�an determinadas por un ciclo. Agradezco me puedan ayudar con el tema que es de mucha urgencia. Saludos, David C. [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R-es] Rmysql
Gracias, Javier. Alguien tiene experiencia con Mac? Que tal la comunicacion en OS X? Saludos, Jorge Velez.- 2015-02-26 0:54 GMT+11:00 Javier Marcuzzi javier.ruben.marcu...@gmail.com: Estimados No es una pregunta, pero si una buena noticia, le� que R y mysql en windows ahora se llevan bien. https://github.com/rstats-db/RMySQL/releases/tag/v0.10 Javier Rub�n Marcuzzi [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R-es] Problema con bucle for
O solo R sum(combn(x, 2, prod)) [1] 14121 Saludos, Jorge.- 2015-02-24 21:00 GMT+11:00 Carlos Ortega c...@qualityexcellence.es: Hola, Otra forma de hacerlo, es as�: #--- x- c(24,12,45,68,45) sum(apply(combn(x,2),2,prod)) [1] 14121 #--- Y te ahorras los l�os del bucle y de los �ndices... Saludos, Carlos Ortega www.qualityexcellence.es El 24 de febrero de 2015, 10:36, Francisco Rodr�guez fjr...@hotmail.com escribi�: Si he entendido bien el problema, lo que quieres hacer realmente es esto: x- c(24,12,45,68,45)n-length(x)res=0for(i in 2:n-1){ for(j in (i+1):n){res- res + (x[i]*x[j])print(res) }} Cuya salida es: [1] 288[1] 1368[1] 3000[1] 4080[1] 4620[1] 5436[1] 5976[1] 9036[1] 11061[1] 14121 Varias observaciones: 1:n-1 define un vector que empieza en 0, cuando la posici�n 1 en R es el 1 por tanto x[0] no existe y no da resultado sum(x[i]*x[j]) es una operaci�n que calcula la suma de un vector, en tu caso tienes un escalar y por tanto no deber�a hacer nada Tienes que definir res para que se sume as� misma Un saludo Francisco J. Date: Tue, 24 Feb 2015 10:02:11 +0100 From: mora...@us.es To: r-help-es@r-project.org Subject: [R-es] Problema con bucle for Hola, quiero obtener la suma del producto de los elementos de un vector y cuando construyo el c�digo me aparecen una serie de NA que me impiden calcular la suma. �Alguna sugerencia? El c�digo es el siguiente: x- c(24,12,45,68,45) n-length(x) res-numeric() for(i in 1:n-1){ for(j in i+1:n){ res- sum(x[i]*x[j]) print(res) } } res [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es -- Saludos, Carlos Ortega www.qualityexcellence.es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R-es] intercalar elementos de vectores
Fernando, Podrias intentar R a - rep('a', 5) R b - rep('b', 5) R a [1] a a a a a R b [1] b b b b b R c(rbind(a, b)) [1] a b a b a b a b a b Saludos, Jorge.- 2015-02-24 23:49 GMT+11:00 Fernando Macedo ferm...@gmail.com: Buenas a todos. Relato el problema: - tengo un archivo de 316 columnas por 562000 filas (aprox.). - esas 316 columnas representan 158 sujetos, o sea dos columnas por cada individuo conteniendo informaci�n que debe ser condensada en una sola. Lo que necesito es ir tomando las dos columnas de cada individuo e intercalar los elementos de los vectores formando uno solo. Ejemplificando ser�a algo as�: a [1] a a a a a b [1] b b b b b c [1] a b a b a b a b a b Estoy haciendo con un loop for pero es realmente muy lento. He buscado por alg�n paquete que ya lo haga directamente pero no he tenido mucho �xito. Me imagino que con sapply o apply pueda ser mucho m�s efectivo pero me ha resultado complicado para entender la sintaxis de estas funciones cuando involucra m�s de un objeto (vector, matriz, etc...). Desde ya agradezco las sugerencias que puedan verter sobre este problema. -- Fernando Macedo [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R-es] intercalar elementos de vectores
Gracias, Carlos. Habia pensado en algo similar usando sapply(): sapply(seq(1, ncol(vtmp), by = 2), function(i) c(rbind(as.character(vtmp[, i]), as.character(vtmp[, i+1] Dependiendo de la dimension de los datos, quizas mapply() sea mas eficiente que sapply(). Saludos cordiales, Jorge.- 2015-02-25 1:01 GMT+11:00 Carlos Ortega c...@qualityexcellence.es: Hola, Este otro ejemplo a partir de la idea de Jorge, de c�mo procesar toda la tabla que tienes: #Creo un data.frame de ejemplo todo con letras vtmp - as.data.frame(lapply(letters,function(x) { rep(x,each=50) })) names(vtmp) - paste(col,LETTERS,sep=) head(vtmp) colA colB colC colD colE colF colG colH colI colJ colK colL colM colN colO colP colQ colR colS colT colU colV colW colX colY colZ 1abcdefghijklmn opqrstuvwxyz 2abcdefghijklmn opqrstuvwxyz 3abcdefghijklmn opqrstuvwxyz 4abcdefghijklmn opqrstuvwxyz 5abcdefghijklmn opqrstuvwxyz 6abcdefghijklmn opqrstuvwxyz #Los n�meros de columnas impares de mi data.frame matIndex - seq(1, dim(vtmp)[2], by=2) #Funci�n para juntar dos columnas (idea de Jorge) mifun - function(x,y) c(rbind(as.vector(vtmp[,x]),as.vector(vtmp[,y]))) #Resulado aplicando mapply. Coge dos columnas consecutivas y las alterna #y as� de forma iterativa para cada una de las columnas que indica matIndex y la siguiente columna matIndex+1 resultado - mapply(mifun,matIndex, matIndex+1) head(kk) [,1] [,2] [,3] [,4] [,5] [,6] [,7] [,8] [,9] [,10] [,11] [,12] [,13] [1,] a c e g i k m o q s u w y [2,] b d f h j l n p r t v x z [3,] a c e g i k m o q s u w y [4,] b d f h j l n p r t v x z [5,] a c e g i k m o q s u w y [6,] b d f h j l n p r t v x z Saludos, Carlos Ortega www.qualityexcellence.es El 24 de febrero de 2015, 14:10, Fernando Macedo ferm...@gmail.com escribi�: Excelente! Ahora corre muy r�pido. No conoc�a ese m�todo, creo que me va a resultar muy �til. Muchas gracias y saludos. Fernando Macedo El 24/02/15 a las 10:51, Jorge I Velez escribi�: Fernando, Podrias intentar R a - rep('a', 5) R b - rep('b', 5) R a [1] a a a a a R b [1] b b b b b R c(rbind(a, b)) [1] a b a b a b a b a b Saludos, Jorge.- 2015-02-24 23:49 GMT+11:00 Fernando Macedo ferm...@gmail.com: Buenas a todos. Relato el problema: - tengo un archivo de 316 columnas por 562000 filas (aprox.). - esas 316 columnas representan 158 sujetos, o sea dos columnas por cada individuo conteniendo informaci�n que debe ser condensada en una sola. Lo que necesito es ir tomando las dos columnas de cada individuo e intercalar los elementos de los vectores formando uno solo. Ejemplificando ser�a algo as�: a [1] a a a a a b [1] b b b b b c [1] a b a b a b a b a b Estoy haciendo con un loop for pero es realmente muy lento. He buscado por alg�n paquete que ya lo haga directamente pero no he tenido mucho �xito. Me imagino que con sapply o apply pueda ser mucho m�s efectivo pero me ha resultado complicado para entender la sintaxis de estas funciones cuando involucra m�s de un objeto (vector, matriz, etc...). Desde ya agradezco las sugerencias que puedan verter sobre este problema. -- Fernando Macedo [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es -- Saludos, Carlos Ortega www.qualityexcellence.es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R] Save a plot with a name given as an argument in a function
Hi Evgenia, Try test2 - function(data, TitleGraph){ pdf(paste0(TitleGraph, .pdf), width = 7, height = 5) plot(data) dev.off() } instead. Take a look at ?paste0 for more information. HTH, Jorge.- On Tue, Feb 10, 2015 at 12:14 AM, Evgenia ev...@aueb.gr wrote: test-function(data, TitleGraph){ pdf(TitleGraph.pdf,width=7,height=5) plot(data) dev.off() } test(cars - c(1, 3, 6, 4, 9),TitleGraph=etc) My problem is that I want graph pdf being saved as etc and not as Titlegraph.pdf -- View this message in context: http://r.789695.n4.nabble.com/Save-a-plot-with-a-name-given-as-an-argument-in-a-function-tp4702965.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R-es] Listas Recursivas
Hola Jesus, Intenta lo siguiente, donde x es tu w.list: R unlist(sapply(x, '[', 'a')) a a 1 11 R unlist(sapply(x, '[', 'b')) b1 b2 b3 b4 b5 b1 b2 b3 b4 b5 41 42 43 44 45 71 72 73 74 75 R unlist(sapply(x, '[', 'c')) c c X Z Saludos, Jorge.- 2015-01-29 22:37 GMT+11:00 Jesus Herranz jesus.herr...@imdea.org: Hola Tengo un an�lisis en el que como resultado obtengo una lista recursiva, es decir, una lista cuyos componentes a su vez son listas. Son varias iteraciones de una funci�n que proporciona varios resultados de inter�s de distinta naturaleza y por eso los uno en una lista. Las iteraciones son hechas en paralelo, y los resultados de una funci�n de este tipo suelen ser empaquetados a su vez en una lista (Por ejemplo, ClusterApply del paquete snow ) Bueno, esto es para poner en situaci�n el problema, porque el tema es mucho m�s general, y he tratado de sacarlo del contexto para que se pueda entender mejor. Digamos que tengo lo siguiente: una lista que tiene 2 listas con los mismos nombres de componentes (cada una de ellas ser�an los resultados de una iteraci�n). Lo que quiero es tener vectores separados para a, b y c. El problema parece sencillo, pero no encuentro la forma de acceder bien a los elementos de la lista; y con el unlist lo junta todo y lo convierte a car�cter, lo que no es muy �til. Al final, la �nica soluci�n que he encontrado es con un for, pero me gustar�a hacerlo de otra forma. Gracias w.list = list ( list ( a = 1 , b = 41:45 , c = c(X) ) , list ( a = 11 , b = 71:75 , c = c(Z) ) ) w.list a.all = NULL ; b.all = NULL; c.all = NULL for ( i in 1:2 ) { a.all = c ( a.all , w.list[[i]]$a ) b.all = c ( b.all , w.list[[i]]$b ) c.all = c ( c.all , w.list[[i]]$c ) } a.all b.all c.all [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R-es] Error seleccionando modelos por el AIC
Hola Javier, La ayuda de ?aictabe establece que la implementacion del AIC no funciona para objetos de clase lmer (a la que pertenecen los modelos ajustados). De ahi el error. La idea es simplemente comparar los modelos basados en el AIC? O tienes pensado algo mas? Si es lo primero, los objetos lmer contienen el AIC -- es solo cuestion de usar str(objetolmer), ubicar la entrada correspondiente y extraerla. Si es lo segundo, ahi tendrias tu que ayudarnos con un poco mas de informacion. Saludos cordiales, Jorge.- On Fri, Jan 30, 2015 at 6:37 AM, javier bueno enciso jbuenoenc...@hotmail.com wrote: Hola, os escribo por un error que me sale al tratar de hacer una tabla de selecci�n de modelos seg�n AIC, con la funci�n aictab() del paquete AICcmodavg. Os copio aqu� el script que utilizo: m1- lmer(ldate~A+(1|zona/area), data=Data, subset= (Data$brood== 0)) m2-lmer(ldate~B+(1|zona/area), data=Data, subset= (Data$brood== 0)) m3-lmer(ldate~A*B+(1|zona/area),data=Data, subset= (Data$brood== 0)) m4-lmer(ldate~A+B+(1|zona/area), data=Data, subset= (Data$brood== 0)) m5-lmer(ldate~A+A*B+(1|zona/area), data=Data, subset= (Data$brood== 0)) m6-lmer(ldate~B+A*B+(1|zona/area), data=Data, subset= (Data$brood== 0)) m7-lmer(ldate~A+B+A*B+(1|zona/area), data=Data, subset= (Data$brood== 0)) ## library(AICcmodavg, lib.loc=~/R/win-library/3.1) modList - list(glm1, glm2, glm3, glm4, glm5, glm6, glm7) Modnames- c(1, 2, 3, 4, 5, 6, 7) aictab(cand.set= modList, modnames= Modnames) El error es el siguiente: Error in aictab.default(cand.set = modList, modnames = ModNames) : Function not yet defined for this object classHe buscado por internet pero no consigo solucionarlo, cualquier ayuda ser�a de gran utilidad. Muchas gracias. [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R-es] Ajuste con exponencial
Hola Hector, No soy experto, pero en http://r.789695.n4.nabble.com/Fitting-weibull-exponential-and-lognormal-distributions-to-left-truncated-data-td869977.html http://www.r-bloggers.com/r-help-follow-up-truncated-exponential/ http://www.jstatsoft.org/v16/c02/paper hay algunas ideas.Espero te sirvan. Saludos, Jorge.- 2015-01-28 22:27 GMT+11:00 Hector G�mez Fuerte hect...@gmx.es: Saludos cordiales. Lamentablemente lo que dice Carlos no es correcto. Cuando la distribuci�n exponencial la truncamos en un intervalo la constante de la funci�n de densidad (para que integre 1) tiene una dependencia (complicada) del par�metro que multiplica al exponente, con lo cual la ecuaci�n de verosimilitud no es nada sencila ni se puede resolver exactamente . La estimaci�n maximo-veros�mil requerir�a de algoritmos num�ricos y por tanto de software para su c�lculo. Yo creo que esta no debe ser la mejor soluci�n, y me sorprende no haber encontrado (puede que por mi torpeza) nada para ello en el R. De aqu� la pregunta que hacia en este foro. H�ctor G�mez *Enviar:* martes 27 de enero de 2015 a las 22:07 *De:* Carlos J. Gil Bellosta c...@datanalytics.com *Para:* Hector G�mez Fuerte hect...@gmx.es *CC:* Lista R r-help-es@r-project.org *Asunto:* Re: [R-es] Ajuste con exponencial Hola, �qu� tal? Creo que el ajuste (por m�xima verosimilitud) de lambda es el inverso de la media de tus datos. Tu densidad en el intervalo de inter�s es como la de la exponencial (dividida por una constante de normalizaci�n). El logaritmo de la verosimitud es, por lo tanto, como el de la exponencial sin truncar m�s una constante. Luego la teor�a habitual (de c�mo el inverso de la media es el estimador por MV de lambda) aplica con cambios m�nimos. Un saludo, Carlos J. Gil Bellosta http://www.datanalytics.com El d�a 27 de enero de 2015, 19:53, Hector G�mez Fuerte hect...@gmx.es escribi�: Buenas tardes, �c�mo puedo con el R ajustar una distribuci�n exponencial truancada (en el intervalo [10,60]) a un vector de datos? Muchas gracias. H�ctor G�mez ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R-es] Simulación de modelo logit con interacción
Gracias Emilio por el codigo en R. Hace poco estuve revisando una situacion similar a la que ilustras en tu mensaje (i.e., seleccion de modelos e inferencia). Dale una mirada a estos dos articulos: http://www-stat.wharton.upenn.edu/~berkr/PoSI-submit.pdf http://statweb.stanford.edu/~ckirby/brad/papers/2013ModelSelection.pdf Saludos cordiales, Jorge.- 2015-01-24 6:24 GMT+11:00 Emilio Torres Manzanera tor...@uniovi.es: �Mil gracias! Parece que el tama�o �s� que importa! aunque solo sean 4 coeficientes. Para estimar los 4 coeficientes con cierta precisi�n, se necesitan unos 2000 datos. La pr�xima vez que vea un estudio sobre si trabaja (s�/no) en funci�n del sexo (hombre/mujer) y estado civil (casado/soltero), con un tama�o de muestra de n=400, no s� qu� le dir� al autor... Respecto a los coeficientes, la verdad es que eran feos (eso me pasa por copiar y pegar de otros ejemplos). Pongo abajo el c�digo definitivo que he utilizado para que mi pobre ordenador pudiera correr la simulaci�n en un tiempo razonable. �Gracias de nuevo! Emilio ## library(dplyr) ## funcion auxiliar hacertablafrecuencias - function(x, vars=NULL, freq=NULL, sort=FALSE){ evaldp - function (.data, .fun.name, ..., .envir = parent.frame()) { args - list(...) args - partial_eval(args, env = .envir) args - unlist(args) if (is.function(.fun.name)) .fun.name - as.character(substitute(.fun.name)) code - paste0(.fun.name, (.data,, paste0(args, collapse = ,), )) eval(parse(text = code, srcfile = NULL)) } if(is.null(vars)) vars - names(x) nms - c(vars, freq) nmsfreq - make.names(nms, unique = TRUE)[length(nms)] if (is.null(freq)) { action - paste( nmsfreq,=n() ) ##quote(n()) } else { if(freq==freq) nmsfreq - freq vars - vars[ vars!= freq] action - paste(nmsfreq, =sum(,freq,)) } if(nmsfreq!=freq) warning(paste(freq column:,nmsfreq)) out - group_by_(x, .dots = vars) %% evaldp(summarise, action) %% ungroup() if (!sort) { out } else { arrange(out, desc(freq)) } } ## modelo : -1 - 4 * dat$x1 + 7 * dat$x2 - 1 *dat$x1* dat$x2 logisticsimulation - function(n){ dat - data.frame(x1=rep(c(0:1), each=n), x2=rep(c(0:1), time=n)) odds - exp(-1 - 4 * dat$x1 + 7*dat$x2 - 1 *dat$x1* dat$x2 ) pr - odds/(1+odds) res - replicate(100, { dat$y - rbinom(2*n,1,pr) ttdat - hacertablafrecuencias(dat) coef(glm(y ~ x1*x2, data = ttdat, family = binomial(),weights=ttdat$freq)) }) t(res) } res - logisticsimulation(400) # son en realidad 800 datos. Ajusta de pena apply(res,2,median) ## (Intercept) x1 x2 x1:x2 ## -0.9946226 -4.2473363 19.8453136 -0.5429929 res - logisticsimulation(1000) # son en realidad 2000 datos. Buen ajuste apply(res,2,median) ## (Intercept) x1 x2 x1:x2 ## -1.004794 -4.1203707.243097 -1.267561 On Friday, January 23 2015, 12:23:25, Olivier Nu�ez onu...@unex.es wrote: Efectivamente, la normalidad tarda en llegar (un problema que merece ser investigado) En cualquier caso, parece que ambos dise�os dan varianzas asint�ticas similares de los estimadores: n=1000 dat - data.frame(x1=sample(0:1, n,replace=TRUE),x2=sample(0:1, n,replace=TRUE)) dat$intercept=1 odds - exp(-1 - 4 * dat$x1 + 7*dat$x2 - 1 *dat$x1* dat$x2 ) pr - odds/(1+odds) D=diag(pr*(1-pr)) X=as.matrix(dat) I=t(X)%*%D%*%X solve(I) x1 x2intercept x1 0.4716741 -0.451997095 -0.014952896 x2-0.4519971 0.470731667 -0.005214237 intercept -0.0149529 -0.005214237 0.020017370 dat$x1= rep(c(0,1), each = n/2) dat$x2 = rep(c(0,1), times = n/2) odds - exp(-1 - 4 * dat$x1 + 7*dat$x2 - 1 * dat$x1 * dat$x2 ) pr - odds/(1+odds) D=diag(pr*(1-pr)) X=as.matrix(dat) I=t(X)%*%D%*%X solve(I) x1 x2intercept x1 0.45113285 -0.430788206 -0.014755274 x2-0.43078821 0.451132851 -0.005589371 intercept -0.01475527 -0.005589371 0.020161833 - Mensaje original - De: Carlos J. Gil Bellosta c...@datanalytics.com Para: Olivier Nu�ez onu...@unex.es CC: Emilio Torres Manzanera tor...@uniovi.es, r-help-es r-help-es@r-project.org Enviados: Viernes, 23 de Enero 2015 11:14:44 Asunto: Re: [R-es] Simulaci�n de modelo logit con interacci�n Hola, �qu� tal? Cierto, cierto, hab�a un error en el c�digo que publiqu�. Pero el diagn�stico es parecido. Cuando los datos se generan con el coeficiente de x2 igual a 7, los coeficientes estimados tienen una distribuci�n extra�a, bimodal (aparentemente), en lugar de _normalmente_ distribuida alrededor del 7 como se espera. Supongo que depende del n�mero de casos en que x2 = 1 e y = 0. Un saludo, Carlos J. Gil Bellosta
[R] Proportion of equal entries in dist()?
Dear all, Given vectors x and y, I would like to compute the proportion of entries that are equal, that is, mean(x == y). Now, suppose I have the following matrix: n - 1e2 m - 1e4 X - matrix(sample(0:2, m*n, replace = TRUE), ncol = m) I am interested in calculating the above proportion for every pairwise combination of rows. I came up with the following: myd - function(X, p = NROW(X)){ D - matrix(NA, p, p) for(i in 1:p) for(j in 1:p) if(i j) D[i, j] - mean(X[i, ] == X[j,]) D } system.time(d - myd(X)) However, in my application n and m are much more larger than in this example and the computational time might be an issue. I would very much appreciate any suggestions on how to speed the myd function. Note: I have done some experiments with the dist() function and despite being much, much, much faster than myd, none of the default distances fits my needs. I would also appreciate any suggestions on how to include my own distance function in dist(). Thank you very much for your time. Best regards, Jorge Velez.- [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R-es] Abreviar nombres ciéntificos
Hola Juan Carlos, Quizas lo siguiente pueda serte util: # test R s - Merluccius merluccius R strsplit(s, ) [[1]] [1] Merluccius merluccius R strsplit(s, )[[1]] [1] Merluccius merluccius R s - strsplit(s, )[[1]] R paste0(substr(s[1], 1, 1), ., s[2]) [1] M.merluccius # funcion convertir - function(s){ s - strsplit(s, )[[1]] paste0(substr(s[1], 1, 1), ., s[2]) } convertir - Vectorize(convertir) s - c(Merluccius merluccius, Hymenocephalus italicus) convertir(s) # Merluccius merluccius Hymenocephalus italicus # M.merlucciusH.italicus Saludos, Jorge.- 2015-01-13 8:34 GMT+11:00 JC Arronte j_arro...@hotmail.com: Hola a tod@s, Estoy tratando de abreviar nombres ci�ntificos pero no me gusta c�mo queda usando make.cepnames de la librer�a vegan. Me gustar�a poderlos abreviar as�, Hymenocephalus italicus -- H.italicus Merluccius merluccius -- M.merluccius He probado con varias opciones y no consigo dar con ello. Estoy casi seguro de que es algo relativamente sencillo, pero no doy con ello. �Alguien podr�a echarme una mano?. Un saludo y gracias Juan Carlos [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R] non negativity constraints if else function
What about ifelse(w 0, 0, w) See ?ifelse for more information. Best, Jorge.- On Sat, Dec 20, 2014 at 3:26 PM, Esra Ulasan esra_ula...@icloud.com wrote: Hello, I have tried the solve the non-negativity constraint if else function in R. But I have done something wrong because it still gives the same solution. I want that, if weight element is negative set it to zero, else recalculate the weights again. These are the codes: for(i in 1:M){ w[,i] = f+r[i]*g #portfolio weights for(i in 1:M){ if (w 0){w=0}else{w=w} } } If you help me I would be happy Thank you [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] keep only the first value of a numeric sequence
Dear jeff6868, Here is one way: ifelse(with(data, c(0, diff(mydata))) != 1, 0, 1) You could also take a look at ?rle HTH, Jorge.- On Mon, Dec 15, 2014 at 9:33 PM, jeff6868 geoffrey_kl...@etu.u-bourgogne.fr wrote: Hello dear R-helpers, I have a small problem in my algorithm. I have sequences of 0 and 1 values in a column of a huge data frame, and I just would like to keep the first value of each sequences of 1 values, such like in this example: data - data.frame(mydata=c(0,0,0,1,1,1,1,1,0,0,0,0,1,1,1,0,0,0,0,1,1,1,1,1,1,1),final_wished_data=c(0,0,0,1,0,0,0,0,0,0,0,0,1,0,0,0,0,0,0,1,0,0,0,0,0,0)) Any easy way to do this? Thanks everybody! -- View this message in context: http://r.789695.n4.nabble.com/keep-only-the-first-value-of-a-numeric-sequence-tp4700774.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list -- To UNSUBSCRIBE and more, see https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Creating a new column from a series of columns
Dear Dennis, Assuming that your data.frame() is called dd, the following should get you started: colnames(dd[,-1])[apply(dd[,-1], 1, function(x) which(x == 'Yes'))] HTH, Jorge.- On Sat, Nov 1, 2014 at 12:32 PM, Fisher Dennis fis...@plessthan.com wrote: R 3.1.1 OS X Colleagues, I have a dataset containing multiple columns indicating race for subjects in a clinical trial. A subset of the data (obtained with dput) is shown here: structure(list(PLTID = c(7157, 8138, 8150, 9112, 9114, 9115, 9124, 9133, 9141, 9144, 9148, 12110, 12111, 12116, 12134, 12136, 12137, 12142, 12143, 12146, 12147, 13159), Indian..RACE1. = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), Asian..RACE2. = c(, Yes, , , , , , , , , , , , , , , , , , , , ), Black..RACE3. = c(Yes, , , Yes, Yes, Yes, Yes, Yes, , Yes, , , , , , , , Yes, Yes, , , ), Native.Hawaiian.or.other.Pacif..RACE4. = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), White..RACE5. = c(, , Yes, , , , , , Yes, , Yes, Yes, Yes, Yes, Yes, Yes, Yes, , , Yes, Yes, Yes), Other.Race..RACE6. = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA), Specify.Other.Race..RACEOTH. = c(NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA, NA)), .Names = c(PLTID, Indian..RACE1., Asian..RACE2., Black..RACE3., Native.Hawaiian.or.other.Pacif..RACE4., White..RACE5., Other.Race..RACE6., Specify.Other.Race..RACEOTH.), class = data.frame, row.names = 43:64) I would like to add a column that indicates which of the other columns contains Yes. In other words, that column would contain: Black..RACE3. Asian..RACE2. White..RACE5. Black..RACE3. ... Even better would be Black Asian White Black ... (which I can accomplish with strsplit) None of the rows contains more than one 'Yes' although it is possible that none of the entries in a row would be 'Yes' (in which case, the entry in the new column should be NA) I could do this by looping through each of the columns with something like this: DATA$RACE - NA for (COL in 2:8)DATA$RACE[which(DATA[,COL] == Yes)] - names(DATA)[COL] But, I suspect that there is some more elegant way to accomplish this. Thanks in advance. Dennis Dennis Fisher MD P (The P Less Than Company) Phone: 1-866-PLessThan (1-866-753-7784) Fax: 1-866-PLessThan (1-866-753-7784) www.PLessThan.com __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R-es] Matrices complejas
Carlos, Una forma de resolverlo es usando la funcion ?rmultinom Saludos, Jorge.- 2014-10-19 20:32 GMT+11:00 Carlos Hern�ndez-Castellano carlos.hernandezcastell...@gmail.com: Saludos compa�eros/as. Consideren una matriz de n columnas y n filas. Quiero distribuir aleatoriamente 6 n�meros predefinidos (11,12,13,21,22,23,31,32,33,41,42,43), con una probabilidad de aparici�n de cada n�mero que ser� igual a la frecuencia de ese n�mero dividida por la frecuencia total de todos los n�meros. Soy incapaz de resolver el problema: con la funci�n rbinom s�lo puedo generar dos n�meros... Les agradezco enormemente su atenci�n. Gracias, -- *Carlos Hern�ndez-Castellano, CREAF-UAB* [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R-es] Como se hace el operador o (OR) para seleccionar dos o mas niveles de un vector ?
Hola Eric, Revisa ?%in% Saludos, Jorge.- 2014-09-04 8:41 GMT+10:00 eric ericconchamu...@gmail.com: Estimados, tengo un data.frame con una columna que tiene tres diferentes niveles (aunque la columna no es propiamente de un factor, son solo tres letras diferentes), por ejemplo c, t y s, y necesito usar los datos que tienen c o t, como tengo que hacerlo ? Creo que a veces he usado algo asi: dataframe - dataframe[dataframe$columna==c(c,t),] pero por alguna razon, cuando uso esa forma dentro del codigo para crear un grafico, por ejemplo: xyplot(are ~ con | sol, data=datEnd[datEnd$iso==c(c,t),]) el resultado no es correcto. Alguna idea ? Muchas gracias, Eric. -- Forest Engineer Master in Environmental and Natural Resource Economics Ph.D. student in Sciences of Natural Resources at La Frontera University Member in AguaDeTemu2030, citizen movement for Temuco with green city standards for living Nota: Las tildes se han omitido para asegurar compatibilidad con algunos lectores de correo. ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R-es] problema con campos que tienen formato fecha
Hola Angela, Una forma de resolver tu problema es utilizando la funcion difftime. En ?difftime hay varios ejemplos. Observa el argumento units en as.numeric(..., units = 'seconds'). Saludos, Jorge.- 2014-08-26 8:16 GMT+10:00 Angela Andrea Camargo Sanabria angela.andrea.cama...@gmail.com: Hola Javier, Muchas gracias por responder tan r�pido! Yo trabajo en Mac OS X 10.9.4. Versi�n 0.98.953 de RStudio. Versi�n 3.0.2 (2013-09-25) de R. ## Este es el script que estoy trabajando. Se trata de una rutina para automatizar el c�lculo de la duraci�n del evento. setwd(/Users/angelacamargosanabria/Documents/ANGELITA/1-DOC/1-TESIS/4-PAPERS/1-Mamiferos/DATOS/Bases) BASE - read.table(Todos2014.txt, header = TRUE, sep = \t) #attach (BASE) dim (BASE) #library (lubridate) #convertir a formato de fecha y hora BASE - cbind (BASE, (dmy_hms(paste(BASE$Date,BASE$Time,sep= names (BASE)[12]-Time2 #empieza rutina para calcular duraci�n de visitas visitas - data.frame () n-1 narbol - 1 while (n = dim(BASE)[1]) { m = n if (n dim (BASE)[1]) { while (BASE$Registro[m+1] == FALSE m+1 = dim(BASE)[1]) {m=m+1} if (m+1 == dim (BASE)[1] BASE$Registro[m+1]== FALSE) {m+1}} if (m == n) { (visitas[narbol,1]-0) } else {visitas[narbol,1]-(BASE$Time2[m]-BASE$Time2[n])} visitas[narbol,2]-BASE$SpeciesID[n] visitas[narbol,3]-BASE$StationID[n] visitas[narbol,4]-BASE$Time2[n] visitas[narbol,5]-BASE$Time2[m] narbol - narbol + 1 n = m+1 } colnames (visitas) - c(Duracion, Mammal, Arbol, FechaI, FechaF) --- Gracias! *Angela Andrea Camargo Sanabria* Estudiante Doctorado en Ciencias Biol�gicas Laboratorio de Ecolog�a de poblaciones y comunidades tropicales Centro de Investigaciones en Ecosistemas (CIEco) UNAM, campus Morelia Antigua Carretera a P�tzcuaro # 8701 Col. Ex-Hacienda de San Jos� de la Huerta, CP 58190 Morelia, Michoac�n, M�xico Tel.: 443-3222706 ext. 42511 e-mail: aacama...@cieco.unam.mx skype: angela.camargo26 2014-08-25 17:01 GMT-05:00 Marcuzzi, Javier Rub�n javier.ruben.marcu...@gmail.com: Estimada Angela Andrea Camargo Las fechas con R dan trabajo, ser�a bueno que env�es un c�digo R escrito como ejemplo junto a las especificaci�nes de su sistema operativo y versi�n de R (porque me paso tener problemas con las fechas y diferencias entre ambientes de trabajo). Javier Marcuzzi El 25/08/2014 06:46 p.m., Angela Andrea Camargo Sanabria escribi�: Hola a todos, Tengo la siguiente inquietud, espero me puedan ayudar. Tengo una base de datos de la que estoy haciendo varios c?lculos. Uno de ellos es la diferencia entre tiempos para conocer la duraci?n de un evento. Aqu? us? el paquete lubridate para tener fecha y hora en el formato adecuado. Cuando hago el calculo obtengo algo como esto: (BASE$Time2[518]-BASE$Time2[516]) Time difference of 1.97 mins lo cual est? bien. El problema es que quiero guardarlo en un data frame. Pero cuando lo guardo se me pierden las unidades y el formato de fecha. BASEFINAL V1 V2 V3 V4 V5 88 1.97 Eira barbara Posa1 1403765571 1403765689 V1 guarda el resultado de la resta, pero en unos casos son segundos y en otros minutos, c?mo lo puedo saber? Igual me pasa con las columnas V4 y V5 que son la fecha/hora de inicio y de finalizaci?n del respectivo evento. Pero creo que esto ?ltimo lo puedo arreglar si aplico de vuelta la funci?n as.POSIXct(BASEFINAL[,5], origin=1970-01-01). No s? si sea algo tan tonto como que no pueda usar un data frame para guardar mi base final. Agradezco su ayuda!!! Saludos, *Angela Andrea Camargo Sanabria* Estudiante Doctorado en Ciencias Biol?gicas Laboratorio de Ecolog?a de poblaciones y comunidades tropicales Centro de Investigaciones en Ecosistemas (CIEco) UNAM, campus Morelia Antigua Carretera a P?tzcuaro # 8701 Col. Ex-Hacienda de San Jos? de la Huerta, CP 58190 Morelia, Michoac?n, M?xico Tel.: 443-3222706 ext. 42511 e-mail: aacama...@cieco.unam.mx skype: angela.camargo26 [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org
Re: [R] print vectors with consecutive numbers
Hi James, Try mat[, apply(mat, 2, function(x) any(diff(x) == 1))] HTH, Jorge.- On Fri, Aug 22, 2014 at 10:18 PM, James Wei zwei0...@hotmail.com wrote: Hi all, I have a matrix with consecutive and non-consecutive numbers in columns. For example, the first 2 columns have consecutive numbers. I want R to print only columns with consecutive numbers. Here is the matrix and how I did using conditional statement: ## mat=matrix(data=c(9,2,3,4,5,6,10,13,15,17,19,22, 25,27,29,31,34,37,39,41),ncol=5) mat difference = diff(mat)==1 difference y1=difference[1,] y2=difference[2,] y3=difference[3,] y=(y1|y2|y3) y if (y==TRUE) mat else 0 ## However, R still print all 5 columns, not the first 2 columns I wanted. I got the Warning message: In if (y == TRUE) mat else 0 : the condition has length 1 and only the first element will be used How can I change the code to get only the first 2 columns with consecutive numbers printed? I am new to R. Thanks in advance for your help. James [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R-es] pregunta
Buenas noches Javier y Jos�, Estoy en contra de usar attach(), asi que propongo la siguiente alternativa con with(): # paquete require(epicalc) # los argumentos en ... pasan de epicalc:::cc # ver ?cc para mas informacion foo - function(var1, var2, var3, ...){ or1 - cc(var1, var2, ...) or2 - cc(var1, var3, ...) list(or1 = or1, or2 = or2) } # datos x - read.csv(~/Downloads/OR.csv) head(x) # resultados SIN graficas with(x, foo(estado, cake, chocolate, graph = FALSE)) Saludos, Jorge.- 2014-08-21 12:40 GMT+10:00 Javier Marcuzzi javier.ruben.marcu...@gmail.com : Estimado Jos� Betancourt Copio y pego una forma donde anda, b�sicamente es lo mismo pero con una peque�a diferencia, es tan parecido que est�n los dos c�digos a continuaci�n. Javier Marcuzzi library(epicalc) #Comando que llama a una funci�n rm(list=ls()) #setwd(D:/DEMO_new/demo_scripts/OR/) #setwd(D:/Public/Documents/R/EPICALC/funciones/OR/) #data= mydata-read.csv(OR.csv,header=TRUE, sep=,, dec=.) data - read.csv(~/Descargas/OR.csv,header=TRUE, sep=,, dec=.) data2 - read.csv(~/Descargas/OR.csv,header=TRUE, sep=,, dec=.) use(data) attach(data) var1=estado var2=cake var3=chocolate # source(function_or.r) #funci�n odratios - function (data,var1,var2,var3){ or1 -cc(var1, var2) or2 - cc(var1, var3) } odratios(data,var1,var2,var3) odratios2 - function (data,estado,cake,chocolate){ or1 -cc(estado, cake) or2 - cc(estado, chocolate) } odratios2(data2,estado,cake,chocolate) El 20 de agosto de 2014, 21:10, Dr. Jos� A Betancourt Bethencourt jbetanco...@iscmc.cmw.sld.cu escribi�: Estimados Estoy entrenando hacer funciones que respondan a comandos, en esta caso en la salida gr�fica se observa que dice : Exposure=var3 y outcome=var 1 quisi�ramos que se reflejan los nombres de la base de datos : var1=estado, var2=cake, var3=chocolate Espero haberme explicado adecuadamente Adjunto tabla con datos #Comando que llama a una funci�n rm(list=ls()) #setwd(D:/DEMO_new/demo_scripts/OR/) #setwd(D:/Public/Documents/R/EPICALC/funciones/OR/) data= mydata-read.csv(OR.csv,header=TRUE, sep=,, dec=.) use(data) attach(data) var1=estado var2=cake var3=chocolate library(epicalc) source(function_or.r) odratios(data,var1,var2,var3) #funci�n odratios - function (data,var1,var2,var3){ or1 -cc(var1, var2) or2 - cc(var1, var3) } ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R-es] [xtable]
Hola Ernesto, Te sugiero trabajar con el paquete texreg: Philip Leifeld (2013). texreg: Conversion of Statistical Model Output in R to LaTeX and HTML Tables. Journal of Statistical Software, 55(8), 1-24. URL http://www.jstatsoft.org/v55/i08/. Saludos, Jorge.- 2014-08-19 9:26 GMT+10:00 Ernesto silvero lazaga ernestsilver3...@gmail.com : Hola Comunidad grandiosa Estoy trabajando con el paquete survival. 1.) Y estoy queriendo usar el paquete xtable en sweave y dice que es incompatible con (por ejemplo summary(coxph)) 2.) y como no me sale estoy tratando de llamar a un valor de summary(coxph) con /Sexpr tampoco funciona. Bueno si algunos de vosotros pueden ayudarme. Viva comunidad -- ernesto [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R] data.table/ifelse conditional new variable question
Dear Kate, Try this: res - do.call(rbind, lapply(xs, function(l){ l$PID - l$MID - 0 father - with(l, Relationship == 'father') mother - with(l, Relationship == 'mother') if(sum(father) == 0) l$PID[l$Relationship == 'sibling'] - 0 else l$PID[l$Relationship == 'sibling'] - l$Sample.ID[father] if(sum(mother) == 0) l$MID[l$Relationship == 'sibling'] - 0 else l$MID[l$Relationship == 'sibling'] - l$Sample.ID[mother] l })) It is assumed that when either parent is not available the M/PID is 0. Best, Jorge.- On Sun, Aug 17, 2014 at 10:58 AM, Kate Ignatius kate.ignat...@gmail.com wrote: Actually - I didn't check this before, but these are not all nuclear families (as I assumed they were). That is, some don't have a father or don't have a mother Usually if this is the case PID or MID will become 0, respectively, for the child. How can the code be edit to account for this? On Sat, Aug 16, 2014 at 8:02 PM, Kate Ignatius kate.ignat...@gmail.com wrote: Thanks! I think I know what is being done here but not sure how to fix the following error: Error in l$PID[l$\Relationship == sibling] - l$Sample.ID[father] : replacement has length zero On Sat, Aug 16, 2014 at 6:48 PM, Jorge I Velez jorgeivanve...@gmail.com wrote: Dear Kate, Assuming you have nuclear families, one option would be: x - read.table(textConnection(Family.ID Sample.ID Relationship 14 62 sibling 14 94 father 14 63 sibling 14 59 mother 17 6004 father 17 6003 mother 17 6005 sibling 17 368 sibling 130 202 mother 130 203 father 130 204 sibling 130 205 sibling 130 206 sibling 222 9 mother 222 45 sibling 222 34 sibling 222 10 sibling 222 11 sibling 222 18 father), header = TRUE) closeAllConnections() xs - with(x, split(x, Family.ID)) res - do.call(rbind, lapply(xs, function(l){ l$PID - l$MID - 0 father - with(l, Relationship == 'father') mother - with(l, Relationship == 'mother') l$PID[l$Relationship == 'sibling'] - l$Sample.ID[father] l$MID[l$Relationship == 'sibling'] - l$Sample.ID[mother] l })) res HTH, Jorge.- Best regards, Jorge.- On Sun, Aug 17, 2014 at 5:42 AM, Kate Ignatius kate.ignat...@gmail.com wrote: Hi, I have a data.table question (as well as if else statement query). I have a large list of families (file has 935 individuals that are sorted by famiy of varying sizes). At the moment the file has the columns: SampleID FamilyID Relationship To prevent from having to make a pedigree file by hand - ie adding a PaternalID and a MaternalID one by one I want to try write a script that will quickly do this for me (I eventually want to run this through a program such as plink) Is there a way to use data.table (maybe in conjucntion with ifelse to do this effectively)? An example of the file is something like: Family.ID Sample.ID Relationship 14 62 sibling 14 94 father 14 63 sibling 14 59 mother 17 6004 father 17 6003 mother 17 6005 sibling 17 368 sibling 130 202 mother 130 203 father 130 204 sibling 130 205 sibling 130 206 sibling 222 9 mother 222 45 sibling 222 34 sibling 222 10 sibling 222 11 sibling 222 18 father But the goal is to have a file like this: Family.ID Sample.ID Relationship PID MID 14 62 sibling 94 59 14 94 father 0 0 14 63 sibling 94 59 14 59 mother 0 0 17 6004 father 0 0 17 6003 mother 0 0 17 6005 sibling 6004 6003 17 368 sibling 6004 6003 130 202 mother 0 0 130 203 father 0 0 130 204 sibling 203 202 130 205 sibling 203 202 130 206 sibling 203 202 222 9 mother 0 0 222 45 sibling 18 9 222 34 sibling 18 9 222 10 sibling 18 9 222 11 sibling 18 9 222 18 father 0 0 I've tried searches for this but with no luck. Greatly appreciate any help - even if its just a link to a great example/solution! Thanks! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R
Re: [R] data.table/ifelse conditional new variable question
Perhaps I am missing something but I do not get the same result: x - read.table(textConnection(Family.ID Sample.ID Relationship 2702 349 mother 2702 3456 sibling 2702 9980 sibling 3064 3 father 3064 4 mother 3064 5sibling 3064 86 sibling 3064 87 sibling), header = TRUE) closeAllConnections() xs - with(x, split(x, Family.ID)) res - do.call(rbind, lapply(xs, function(l){ l$PID - l$MID - 0 father - with(l, Relationship == 'father') mother - with(l, Relationship == 'mother') if(sum(father) == 0) l$PID[l$Relationship == 'sibling'] - 0 else l$PID[l$Relationship == 'sibling'] - l$Sample.ID[father] if(sum(mother) == 0) l$MID[l$Relationship == 'sibling'] - 0 else l$MID[l$Relationship == 'sibling'] - l$Sample.ID[mother] l })) #Family.ID Sample.ID Relationship MID PID #2702.1 2702 349 mother 0 0 #2702.2 2702 3456 sibling 349 0 #2702.3 2702 9980 sibling 349 0 #3064.4 3064 3 father 0 0 #3064.5 3064 4 mother 0 0 #3064.6 3064 5 sibling 4 3 #3064.7 306486 sibling 4 3 #3064.8 306487 sibling 4 3 HTH, Jorge.- On Sun, Aug 17, 2014 at 11:47 AM, Kate Ignatius kate.ignat...@gmail.com wrote: Yep - you're right - missing parents are indicated as zero in the M/PID field. The above code worked with a few errors: 1: In l$PID[l$Relationship == sibling] - l$Sample.ID[father] : number of items to replace is not a multiple of replacement length 2: In l$PID[l$Relationship == sibling] - l$Sample.ID[father] : number of items to replace is not a multiple of replacement length 3: In l$PID[l$Relationship == sibling] - l$Sample.ID[father] : number of items to replace is not a multiple of replacement length 4: In l$MID[l$Relationship == sibling] - l$Sample.ID[mother] : number of items to replace is not a multiple of replacement length looking at the output I get numbers where the father/mother ID should be in the M/PID field. For example: 2702 349 mother 0 0 2702 3456 sibling 0 842 2702 9980 sibling 0 842 3064 3 father 0 0 3064 4 mother 0 0 3064 5sibling 879 880 3064 86 sibling 879 880 3064 87 sibling 879 880 On Sat, Aug 16, 2014 at 9:31 PM, Jorge I Velez jorgeivanve...@gmail.com wrote: Dear Kate, Try this: res - do.call(rbind, lapply(xs, function(l){ l$PID - l$MID - 0 father - with(l, Relationship == 'father') mother - with(l, Relationship == 'mother') if(sum(father) == 0) l$PID[l$Relationship == 'sibling'] - 0 else l$PID[l$Relationship == 'sibling'] - l$Sample.ID[father] if(sum(mother) == 0) l$MID[l$Relationship == 'sibling'] - 0 else l$MID[l$Relationship == 'sibling'] - l$Sample.ID[mother] l })) It is assumed that when either parent is not available the M/PID is 0. Best, Jorge.- On Sun, Aug 17, 2014 at 10:58 AM, Kate Ignatius kate.ignat...@gmail.com wrote: Actually - I didn't check this before, but these are not all nuclear families (as I assumed they were). That is, some don't have a father or don't have a mother Usually if this is the case PID or MID will become 0, respectively, for the child. How can the code be edit to account for this? On Sat, Aug 16, 2014 at 8:02 PM, Kate Ignatius kate.ignat...@gmail.com wrote: Thanks! I think I know what is being done here but not sure how to fix the following error: Error in l$PID[l$\Relationship == sibling] - l$Sample.ID[father] : replacement has length zero On Sat, Aug 16, 2014 at 6:48 PM, Jorge I Velez jorgeivanve...@gmail.com wrote: Dear Kate, Assuming you have nuclear families, one option would be: x - read.table(textConnection(Family.ID Sample.ID Relationship 14 62 sibling 14 94 father 14 63 sibling 14 59 mother 17 6004 father 17 6003 mother 17 6005 sibling 17 368 sibling 130 202 mother 130 203 father 130 204 sibling 130 205 sibling 130 206 sibling 222 9 mother 222 45 sibling 222 34 sibling 222 10 sibling 222 11 sibling 222 18 father), header = TRUE) closeAllConnections() xs - with(x, split(x, Family.ID)) res - do.call(rbind, lapply(xs, function(l){ l$PID - l$MID - 0 father - with(l, Relationship == 'father') mother - with(l, Relationship == 'mother') l$PID[l$Relationship == 'sibling'] - l$Sample.ID[father] l$MID[l$Relationship == 'sibling'] - l$Sample.ID[mother] l })) res HTH, Jorge.- Best regards, Jorge.- On Sun, Aug 17, 2014 at 5:42 AM, Kate Ignatius kate.ignat...@gmail.com wrote: Hi, I have a data.table question (as well as if else statement query
Re: [R] data.table/ifelse conditional new variable question
Dear Kate, Assuming you have nuclear families, one option would be: x - read.table(textConnection(Family.ID Sample.ID Relationship 14 62 sibling 14 94 father 14 63 sibling 14 59 mother 17 6004 father 17 6003 mother 17 6005 sibling 17 368 sibling 130 202 mother 130 203 father 130 204 sibling 130 205 sibling 130 206 sibling 222 9 mother 222 45 sibling 222 34 sibling 222 10 sibling 222 11 sibling 222 18 father), header = TRUE) closeAllConnections() xs - with(x, split(x, Family.ID)) res - do.call(rbind, lapply(xs, function(l){ l$PID - l$MID - 0 father - with(l, Relationship == 'father') mother - with(l, Relationship == 'mother') l$PID[l$Relationship == 'sibling'] - l$Sample.ID[father] l$MID[l$Relationship == 'sibling'] - l$Sample.ID[mother] l })) res HTH, Jorge.- Best regards, Jorge.- On Sun, Aug 17, 2014 at 5:42 AM, Kate Ignatius kate.ignat...@gmail.com wrote: Hi, I have a data.table question (as well as if else statement query). I have a large list of families (file has 935 individuals that are sorted by famiy of varying sizes). At the moment the file has the columns: SampleID FamilyID Relationship To prevent from having to make a pedigree file by hand - ie adding a PaternalID and a MaternalID one by one I want to try write a script that will quickly do this for me (I eventually want to run this through a program such as plink) Is there a way to use data.table (maybe in conjucntion with ifelse to do this effectively)? An example of the file is something like: Family.ID Sample.ID Relationship 14 62 sibling 14 94 father 14 63 sibling 14 59 mother 17 6004 father 17 6003 mother 17 6005 sibling 17 368 sibling 130 202 mother 130 203 father 130 204 sibling 130 205 sibling 130 206 sibling 222 9 mother 222 45 sibling 222 34 sibling 222 10 sibling 222 11 sibling 222 18 father But the goal is to have a file like this: Family.ID Sample.ID Relationship PID MID 14 62 sibling 94 59 14 94 father 0 0 14 63 sibling 94 59 14 59 mother 0 0 17 6004 father 0 0 17 6003 mother 0 0 17 6005 sibling 6004 6003 17 368 sibling 6004 6003 130 202 mother 0 0 130 203 father 0 0 130 204 sibling 203 202 130 205 sibling 203 202 130 206 sibling 203 202 222 9 mother 0 0 222 45 sibling 18 9 222 34 sibling 18 9 222 10 sibling 18 9 222 11 sibling 18 9 222 18 father 0 0 I've tried searches for this but with no luck. Greatly appreciate any help - even if its just a link to a great example/solution! Thanks! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] reshape a dataset
Dear Sohail, Using Jim's data set skdat, two more options would be # first option d - with(skdat, table(ID, lettertag)) names - colnames(d) d - c(list(rownames(d)), lapply(1:ncol(d), function(i) as.numeric(d[,i]))) names(d) - c('ID', names) d # second option d - with(skdat, table(ID, lettertag)) res - c(list(rownames(d)), sapply(apply(d, 2, list), [, 1)) names(res)[1] - ID res HTH, Jorge.- On Fri, Aug 15, 2014 at 7:19 PM, Jim Lemon j...@bitwrit.com.au wrote: On Thu, 14 Aug 2014 06:08:51 PM Sohail Khan wrote: Hi I have data set as follows: A 92315 A 35018 A 56710 B 52700 B 92315 B 15135 C 35018 C 52700 I would like to transform this data set into: ID 92315 35018 56710 52700 15135 A 1 1 1 0 0 B 1 0 0 1 1 C 0 1 0 1 0 I looked into reshape package to no avail. I would appreciate any suggestions. -Sohail Hi Sohail, You are doing a bit more than reshaping. This may get you there: skdat-read.table(text=A 92315 A 35018 A 56710 B 52700 B 92315 B 15135 C 35018 C 52700,stringsAsFactors=FALSE) names(skdat)-c(lettertag,ID) ID-unique(skdat$ID) lettertags-unique(skdat$lettertag) newskdat-list(ID) for(i in 1:length(lettertags)) newskdat[[i+1]]- as.numeric(ID %in% skdat$ID[skdat$lettertag==lettertags[i]]) names(newskdat)-c(ID,lettertags) I'm assuming that you don't really want your answer as a single string. Jim __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] reshape a dataset
If that's the case, you could do the following: d - with(skdat, table(ID, lettertag)) d - data.frame(cbind(ID = rownames(d), d)) rownames(d) - NULL d HTH, Jorge.- On Fri, Aug 15, 2014 at 8:22 PM, Sohail Khan sohai...@gmail.com wrote: Thanks Jim and Jorge, Clever solutions, the final output is a list. How do I covert it back a dataframe? -Sohail On Fri, Aug 15, 2014 at 5:37 AM, Jorge I Velez jorgeivanve...@gmail.com wrote: Dear Sohail, Using Jim's data set skdat, two more options would be # first option d - with(skdat, table(ID, lettertag)) names - colnames(d) d - c(list(rownames(d)), lapply(1:ncol(d), function(i) as.numeric(d[,i]))) names(d) - c('ID', names) d # second option d - with(skdat, table(ID, lettertag)) res - c(list(rownames(d)), sapply(apply(d, 2, list), [, 1)) names(res)[1] - ID res HTH, Jorge.- On Fri, Aug 15, 2014 at 7:19 PM, Jim Lemon j...@bitwrit.com.au wrote: On Thu, 14 Aug 2014 06:08:51 PM Sohail Khan wrote: Hi I have data set as follows: A 92315 A 35018 A 56710 B 52700 B 92315 B 15135 C 35018 C 52700 I would like to transform this data set into: ID 92315 35018 56710 52700 15135 A 1 1 1 0 0 B 1 0 0 1 1 C 0 1 0 1 0 I looked into reshape package to no avail. I would appreciate any suggestions. -Sohail Hi Sohail, You are doing a bit more than reshaping. This may get you there: skdat-read.table(text=A 92315 A 35018 A 56710 B 52700 B 92315 B 15135 C 35018 C 52700,stringsAsFactors=FALSE) names(skdat)-c(lettertag,ID) ID-unique(skdat$ID) lettertags-unique(skdat$lettertag) newskdat-list(ID) for(i in 1:length(lettertags)) newskdat[[i+1]]- as.numeric(ID %in% skdat$ID[skdat$lettertag==lettertags[i]]) names(newskdat)-c(ID,lettertags) I'm assuming that you don't really want your answer as a single string. Jim __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R-es] leer ficheros excel en R en Ubuntu
Hola Miguel, A que te refieres con y nada? Obtienes algun error? Algun mensaje? Has probado con scan() y/o readLines()? Saludos, Jorge.- 2014-08-15 7:38 GMT+10:00 Miguel Fiandor Guti�rrez miguel.fiandor.gutier...@gmail.com: Hola, Pens� que esto iba a ser trivial en R, pero me estoy encontrado muchos con mi problema en internet, y que las soluciones ofrecidas no terminan de funcionar. Estoy intentando leer un fichero .xls en ubuntu con los siguientes paquetes y nada: require(RODBC) conn = odbcConnectExcel(madrid.xls) # open a connection to the Excel file sqlTables(conn)$TABLE_NAME # show all sheets df = sqlFetch(conn, Sheet1) # read a sheet df = sqlQuery(conn, select * from [Sheet1 $]) # read a sheet (alternative SQL sintax) close(conn) # close the connection to the file require(gdata) xlsfile - file.path(path.package('gdata'),'xls','madrid.xls') df = read.xls (xlsfile) df = read.xls (xlsfile, sheet = 1, header = TRUE) df = read.xls (madrid.xls, sheet = 1, header = TRUE) df = read.xls (madrid.xls) require(xlsx) read.xlsx(madrid.xls, sheetName = Sheet1) library(XLConnect) wk = loadWorkbook(madrid.xls) df = readWorksheet(wk, sheet=Sheet1) -- tambi�n he probado directamente read.table ya que el fichero es tipo xml por dentro: df = read.table(madrid.xls, header = TRUE) -- ejemplo del fichero: $ head -c 500 madrid.xls table border=1trth bgcolor=#FFB18CNombre de la instalacion/thth bgcolor=#FFB18CMunicipio de la instalacion/thth bgcolor=#FFB18CProvincia de la instalacion/thth bgcolor=#FFB18CClave del registro/thth bgcolor=#FFB18CCodigo registro autonomico definitivo/thth bgcolor=#FFB18CPotencia nominal de la fase (kW)/thth bgcolor=#FFB18CGrupo Normativo/thth bgcolor=#FFB18CTipo de Inscripcion/th/trtrtdPERGOLA FOTOVOLTAICA JARDINES COMPLEJO DE MONCLOA ... Gracias de antemano. Por cierto, aprovecho que lanzo la pregunta es para una aplicaci�n Shiny, alguna recomendaci�n de como leer esta info solo una vez al arrancar el servidor? Miguel [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R-es] (sin asunto)
Hola Alfredo, Algunos comentarios/observaciones: 1. No uses attach. Mejor, explora la funcion with() y/o within(). attach es muy peligroso. 2. Solo por curiosidad, como hiciste para crear la tabla usando latabla$ciudad de origen? Supongo que deberia ser latabla$ciudad de origen 3. Lo que observas, tiene que ver con que en tus datos hay espacios escondidos. Una forma de resolver este problema (desde R) es utilizando el paquete stringr y la funcion str_trim. Hay dos ejemplos en la ayuda; creo que el primero de ellos ilustra lo que ocurre en tu caso. Saludos cordiales, Jorge.- 2014-08-12 8:09 GMT+10:00 Alfredo Alvarado david.alvarad...@gmail.com: Buenas tardes grupo, un saludo. Busco su amable ayuda en los siguiente: Tengo una tabla con alrededor de 20 variables en columnas. La tabla proviene de un excel convertido en csv. Estoy tomando dos variables: la columna correspondiente a ciudad de origen y apellido de la persona. hago: attach(latabla) y luego names(latabla), y me da las variables que digo, latabla$ciudad de origen, y latabla$apellido. Quiero ver las dos columnas para ver de acuerdo a la ciudad de origen las frecuencias de los apellidos registrados: table(latabla$ciudad de origen, latabla$apellido) Me da, efectivamente la tabla que quiero, en las filas la ciudad de origen, en las columnas los apellidos, y en los campos, la frecuencia de apellidos por ciudad de origen. Sin embargo, la pregunta que tengo es que la tabla resultante genera una fila sin nombre, y una columna sin nombre, la primera fila y la primera columna, y le asigna un valor de 1, como si hubiera un dato, y al resto 0. Es decir, como si ese campo vacío con esa columna vacía generara un valor. He revisado la tabla, la he cambiado, pero no logrop quitarle eso. Por otra parte, y aún más importante, algunas ciudades, no todas, (de 58, sólo 2), las repite como nombres de filas diferentes, aunque se trata del mismo nombre, lo coloca como si se tratara de dos distintos. Le cambié el nombre en excel y sigue haciendo lo mismo. No tengo idea del por qué sucede esto último. Las otras ciudades las usa como una sola fila y coloca los valores, a excepción de esas dos ciudades que las divide como si fueran diferentes, y las pone una debajo de la otra, dos veces el mismo nombre. ¿Alguna idea que puedan ofrecerme al respecto? De antemano, gracias a todos-. ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R] Possible pair of 2 binary vectors
Dear Ron, What about this? set.seed(123) d - 4 x1 - sample(0:1, d, TRUE) x2 - sample(0:1, d, TRUE) x1 x2 expand.grid(x1 = x1, x2 = x2) See ?expand.grid for more information. Best, Jorge.- On Sat, Aug 9, 2014 at 7:46 PM, Ron Michael ron_michae...@yahoo.com wrote: Hi, Let say I have 2 binary vectors of length 'd', therefore both these vectors can take only 0-1 values. Now I want to simulate all possible pairs of them. Theoretically there will be 4^d possible pairs. Is there any R function to directly simulate them? Thanks for your help. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R-es] Descargar lista de paquetes zipeados
Estimado Prof. Di Rienzo, Creo que lo que busca puede hacerlo con la función download.packages() Saludos cordiales, Jorge.- 2014-07-22 14:03 GMT+10:00 Julio Alejandro Di Rienzo dirienzo.ju...@gmail.com: Hola Alguien sabe como descargar una lista de librerías de R en formato zipeado. Por ejemplo quiero descargar las librerías (lme4, latticeExtras, Biobase,., etc,etc) en formato zipeado. Se que puedo hacerlo una por una desde el cran pero quisiera tener un procedimiento para hacerlo automáticamente. Prof. Julio Di Rienzo Estadística y Biometría FCA- U.N. Córdoba http://sites.google.com/site/juliodirienzo [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R-es] lista de ficheros
Hola Juan, Una forma es la siguiente: 1. Debes decirle a R donde estan los ficheros. Para ello usa setwd() 2. Determina el nombre de los ficheros. Usa list.files() 3. Toma el nombre de cada fichero, leelo y genera el data.frame() que necesitas. Al exportar el data.frame() usando write.table(), utiliza el nombre del fichero correspondiente, pero colocando al principio algo como salida_ y luego el nombre del fichero seguido de .txt o algo similar. 4. Para el grafico, podrias usar pdf() # instrucciones van aqui dev.off() Dentro de pdf() deberias colocar el nombre del fichero con la extension .pdf al final. Supongamos que la funcion f1 toma el NOMBRE de un fichero y lo procesa, y que la funcion f2 toma el NOMBRE del fichero y hace el grafico. Podrias hacer lo siguiente: # set up setwd(...) archivos - list.files(pattern = '.txt') # asumiendo que los archivos son .txt # leerlos info - lapply(archivos, read.table, header = TRUE) # procesarlos lapply(info, f1) # graficos lapply(info, f2) La estuctura de f1 seria algo como f1 - function(nombre){ # hacer proceso info - info[nombre] out - ... # contiene lo que quiero guardar write.table(out, paste0('salida_', nombre, '.txt'), ...) } y f2 seria algo como f2 - function(nombre){ pdf(paste0(nombre, '.pdf') # hacer grafico dev.off() } Algo que podrias hacer es integrar f2 y f1 como a continuacion: f - function(nombre){ # hacer proceso info - info[nombre] out - ... # contiene lo que quiero guardar write.table(out, paste0('salida_', nombre, '.txt'), ...) # grafico pdf(paste0(nombre, '.pdf') # hacer grafico con el objeto info dev.off() } Finalmente usarias setwd(...) archivos - list.files(pattern = '.txt') # asumiendo que los archivos son .txt # leerlos info - lapply(archivos, read.table, header = TRUE) # procesarlos lapply(info, f) Espero sea de utilidad. Saludos, Jorge.- 2014-07-16 20:25 GMT+10:00 Juan Antonio Gil Pascual j...@edu.uned.es: Estimados compañeros tengo que realizar un procedimiento a un conjunto de ficheros con unas mismas características. Todos están colocados en un subdirectorio. Como salida del procedimiento tengo un data.frame que puedo salvar en un fichero y un gráfico que salvaré en un pdf. En total son 144 ficheros. ¿Cómo podría abordar el problema para no tener que crear los 144 procedimientos?. Muchas gracias y un cordial saludo, Juan -- Juan Antonio Gil Pascual Profesor de Metodología de la Investigación Cuantitativa correo: j...@edu.uned.es web: www.uned.es/personal/jgil Dpto. MIDE Facultad de Educación c/Juan del Rosal, 14 desp. 2.72 28040 Madrid Tel'f. 91 3987279 Fax. 91 3987288 ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R] Grouped Boxplot
Dear Anupam, Try boxplot(DISO ~ POS * NODE_CAT, data = yourdata) Another option would be the last example in ?boxplot HTH, Jorge.- On Fri, Jul 11, 2014 at 4:38 PM, anupam sinha anupam.cont...@gmail.com wrote: Dear all, I need some help with plotting boxplots in groups. I have a file of the following format: - POS DISONODE_CAT -- A20Hubs C30Nonhubs B50Nonhubs B10Hubs A25Nonhubs C80Hubs I want to plot boxplots in such a way that DISO is on y-axis and the NODE_CAT is grouped according to POS. If I were to plot the above mentioned case I should get three groups of boxplots A, B, and C. Also each of the POS A, B and C should have two boxplots (one for Hubs and one for Nonhubs). Thanks in advance for any help. Anupam [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R-es] Conversion date a numeric y vuelta a date
Claro, pero y que hacemos con el año 3848503? --JIV 2014-07-10 0:55 GMT+10:00 Alberto Soria alberto.so...@ari-solar.es: Muchas Gracias Jorge y Carlos. Solucionado. No obstante, as.Date(as.numeric(Sys.time())) no me daba error, sino: [1] 3848503-10-22 Un saludo, Alberto. El 9 de julio de 2014, 14:34, Jorge I Velez jorgeivanve...@gmail.com escribió: Hola Alberto, Necesitas as.Date(as.numeric(as.Date(Sys.time())), origin = '1970-01-01') Esta parte as.numeric(as.Date(Sys.time())) # 16260 te da el numero de dias que han transcurrido desde Ene 1, 1970. Luego, utilizando ese dia/año como origen, determinas la fecha actual. Saludos, Jorge.- 2014-07-09 22:25 GMT+10:00 Alberto Soria alberto.so...@ari-solar.es: Hola a todos: Debe de ser una tontería, pero no consigo saber porque la siguiente linea no devuelve la fecha actual: as.Date(as.numeric(Sys.time())) He hecho esa prueba porque no consigo pasar un numero convertido a partir de una fecha y modificado a fecha de nuevo. Gracias por adelantado. Un saludo, Alberto. [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R-es] bucle
Hola Juan Antonio, Has pensado considerar una aproximacion diferente? De ser asi, explora ?cut y ?car:::recode. Saludos, Jorge.- 2014-07-10 16:58 GMT+10:00 juan(uned) j...@edu.uned.es: Estimados compañeros, hoy me ha surgido una duda, quizás trivial, pero que no encuentro sentido. Tengo un bucle con el siguiente código: for (i in 1:n) { if (rango_inr1[i]==1 (inr[i]= 2 inr[i]= 3)) cinr[i]-1 if (rango_inr1[i]==2 (inr[i]= 2.5 inr[i]= 3.5)) cinr[i]-2 if (rango_inr1[i]==3 (inr[i]= 2 inr[i]= 2.9)) cinr[i]-3 if (rango_inr1[i]==4 (inr[i]= 2.25 inr[i]= 3.5)) cinr[i]-4 if (rango_inr1[i]==5 (inr[i]= 2.2 inr[i]= 3.25)) cinr[i]-5 if (rango_inr1[i]==6 (inr[i]= 2 inr[i]= 3.5)) cinr[i]-6 if (rango_inr1[i]==7 (inr[i]= 2 inr[i]= 4)) cinr[i]-7 if (rango_inr1[i]==8 (inr[i]= 2 inr[i]= 2.6)) cinr[i]-8 if (rango_inr1[i]==9 (inr[i]= 2 inr[i]= 2.5)) cinr[i]-9 if (rango_inr1[i]==10 (inr[i]= 2 inr[i]=2.8)) cinr[i]-10 if (rango_inr1[i]==11 (inr[i]= 2.5 inr[i]= 4)) cinr[i]-11 } donde n vale 3738 e i naturalmente 3738. Pues bien, resulta que la variable creada cinr tiene 3737 casos. ¿Qué puede estar ocurriendo?. He comprobado los casos de rango_inr1 y de inr y son 3738. ¿Qué estoy haciendo mal?. Un cordial saludo, Juan -- Juan Antonio Gil Pascual Profesor de Metodología de la Investigación Cuantitativa correo: j...@edu.uned.es web: www.uned.es/personal/jgil Dpto. MIDE Facultad de Educación c/Juan del Rosal, 14 desp. 2.72 28040 Madrid Tel'f. 91 3987279 Fax. 91 3987288 ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R-es] Conversion date a numeric y vuelta a date
Hola Alberto, Necesitas as.Date(as.numeric(as.Date(Sys.time())), origin = '1970-01-01') Esta parte as.numeric(as.Date(Sys.time())) # 16260 te da el numero de dias que han transcurrido desde Ene 1, 1970. Luego, utilizando ese dia/año como origen, determinas la fecha actual. Saludos, Jorge.- 2014-07-09 22:25 GMT+10:00 Alberto Soria alberto.so...@ari-solar.es: Hola a todos: Debe de ser una tontería, pero no consigo saber porque la siguiente linea no devuelve la fecha actual: as.Date(as.numeric(Sys.time())) He hecho esa prueba porque no consigo pasar un numero convertido a partir de una fecha y modificado a fecha de nuevo. Gracias por adelantado. Un saludo, Alberto. [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R-es] abrir varios archivos a la vez y colocarlos en un mismo data frame
Estimado Alejandro, Lo mejor es trabajar con listas, sea creadas antes de o despues de leer los datos (esto ultimo automaticamente desde R). En cuanto a los nombres de las variables, creo que ahorras tiempo y problemas si los incluyes. A continuacion un ejemplo (necesitas el paquete mets): # install.packages('mets') require(mets) files - list.files(path=D:/prueba) info - do.call(rbind, lapply(files, function(x) as.data.frame(fread(x, header = TRUE head(info) Como veras, no uso read.csv(), pero el resultado es el mismo y se obtiene en mucho menos tiempo que con cualquier version de read.*(). Saludos, Jorge.- 2014-07-06 22:32 GMT+10:00 Alejandro J. Estudillo ajestudi...@gmail.com: Buenos tardes, A ver si alguien puede ayudarme. Tengo una carpeta con 20 archivos. Cada uno de estos archivos es un data.frame con las puntuaciones de un participante. Me gustaría escribir una instrucción para que todos estos datos se agrupen en un solo data.frame. El caso es que para el primer de los archivos tendría que leer los headers, pero no para el resto (ya que los headers son los mismos para cada sujeto). He intentado correr el siguiente código files-list.files(path=D:/prueba) for(i in 1:length(files)){ if(i==1){ matriz-read.csv(files [i], header=TRUE) }else{ tmp-read.csv(files[i],header=FALSE) matriz-rbind(matriz,tmp) } } Sin embargo, obtengo el siguiente error: Error in rep(xi, length.out = nvar) : attempt to replicate an object of type 'closure' Alguna idea de que puede estar fallando? Gracias!! Alex [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R-es] error al leer una linea desde un archivo de texto
Gracias por la correccion, Francisco. Siempre escribo esa palabra MAL! :( # aprendiendo a escribir con R practicando_encoding - function(lineas) for(i in 1:lineas) cat(i, --, es 'encoding', NO 'enconding' , \n) practicando_encoding(50) Saludos, Jorge.- 2014-07-04 21:03 GMT+10:00 Francisco Viciana franciscoj.vici...@juntadeandalucia.es: La de Jorge, es la respuesta correcta, aunque le sobraba una n al parámetro encoding = 'latin1' Los dos fichero que ajuntados por Eric son detectados como 'latin-1' por mi emacs, luego la manera correcta de leerlos independiente del operativo, GUI y la configuración del lenguaje de nuestro equipo es: read.csv('d11-18.csv',encoding = 'latin1') El problema proviene del la letra griega mu que se emprea para representar µg (microgramos ? creo recordar ... ). Los encoding y trabajar con mas de un sistema operativo es una fuente permanente de dolor de cabeza. Mi recomendaciones usar siempre que podáis UTF-8. Fran El 03/07/2014 9:57, Jorge I Velez escribió: Hola Eric, Me incliniaria mas por un problema de enconding. Intenta agregando enconding = 'latin1' al final de read.csv() A lo mejor enviandonos tu sessionInfo() podriamos ayudarte un poco mas. Saludos, Jorge.- 2014-07-03 5:32 GMT+10:00 neo ericconchamu...@gmail.com: Estimada comunidad, estoy extrayendo una linea de texto desde varios archivos (unos 200) de esta manera: dat - read.csv(filenames[i], header=FALSE, sep=,, dec=., skip=11, nrows=1) pero al tratar de leer esa linea desde el archivo numero 54 obtengo el siguiente error: Error in type.convert(data[[i]], as.is = as.is[i], dec = dec, na.strings = character(0L)) : invalid multibyte string at 'b5g' Calls: read.csv - read.table - type.convert todos los archivos fueron generados de la misma forma, exportados desde excel usando un breve script de VB par aplicaciones, pero solo algunos me dan ese error, que no se lo que significa, por lo tanto no se como repararlo. Ademas he examinado los archivos y no observo diferencias. Adjunto un archivo que se lee y uno que no se lee, en una de esas se me paso algo por no saber. Alguna idea ? Saludos y muchas gracias, Eric. -- Forest Engineer Master in Environmental and Natural Resource Economics Ph.D. student in Sciences of Natural Resources at La Frontera University Member in AguaDeTemu2030, citizen movement for Temuco with green city standards for living Nota: Las tildes se han omitido para asegurar compatibilidad con algunos lectores de correo. ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es -- +-- | Francisco J. Viciana Fernández | Coordinador del Registro de Población | Servicio de Estadísticas Demográficas y Sociales | Instituto de Estadística y Cartografía de Andalucía | Leonardo Da Vinci, nº 21. Isla de La Cartuja. | 41071 SEVILLA. | franciscoj.vici...@juntadeandalucia.es +-- ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R-es] error al leer una linea desde un archivo de texto
Hola Eric, Me incliniaria mas por un problema de enconding. Intenta agregando enconding = 'latin1' al final de read.csv() A lo mejor enviandonos tu sessionInfo() podriamos ayudarte un poco mas. Saludos, Jorge.- 2014-07-03 5:32 GMT+10:00 neo ericconchamu...@gmail.com: Estimada comunidad, estoy extrayendo una linea de texto desde varios archivos (unos 200) de esta manera: dat - read.csv(filenames[i], header=FALSE, sep=,, dec=., skip=11, nrows=1) pero al tratar de leer esa linea desde el archivo numero 54 obtengo el siguiente error: Error in type.convert(data[[i]], as.is = as.is[i], dec = dec, na.strings = character(0L)) : invalid multibyte string at 'b5g' Calls: read.csv - read.table - type.convert todos los archivos fueron generados de la misma forma, exportados desde excel usando un breve script de VB par aplicaciones, pero solo algunos me dan ese error, que no se lo que significa, por lo tanto no se como repararlo. Ademas he examinado los archivos y no observo diferencias. Adjunto un archivo que se lee y uno que no se lee, en una de esas se me paso algo por no saber. Alguna idea ? Saludos y muchas gracias, Eric. -- Forest Engineer Master in Environmental and Natural Resource Economics Ph.D. student in Sciences of Natural Resources at La Frontera University Member in AguaDeTemu2030, citizen movement for Temuco with green city standards for living Nota: Las tildes se han omitido para asegurar compatibilidad con algunos lectores de correo. ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R] From long to wide format
Hi Arun, Thank you very much for your suggestion. While running some tests, I came across the following: # sample data n - 2000 p - 1000 x2 - data.frame(variable = rep(paste0('x', 1:p), each = n), id = rep(paste0('p', 1:p), n), outcome = sample(0:2, n*p, TRUE), rate = runif(n*p, 0.5, 1)) str(x2) library(dplyr) library(tidyr) # Arun's suggestion system.time({wide1 - x2%% select(-rate) %% mutate(variable=factor(variable, levels=unique(variable)),id=factor(id, levels=unique(id))) %% spread(variable,outcome) colnames(wide1)[-1] - paste(outcome,colnames(wide1)[-1],sep=.) }) # Error: C stack usage 18920219 is too close to the limit # Timing stopped at: 13.833 0.251 14.085 Do you happen to know what can be done to avoid this? Thank you. Best, Jorge.- On Mon, Jun 30, 2014 at 6:51 PM, arun smartpink...@yahoo.com wrote: Hi Jorge, You may try: library(dplyr) library(tidyr) #Looks like this is faster than the other methods. system.time({wide1 - x2%% select(-rate) %% mutate(variable=factor(variable, levels=unique(variable)),id=factor(id, levels=unique(id))) %% spread(variable,outcome) colnames(wide1)[-1] - paste(outcome,colnames(wide1)[-1],sep=.) }) #user system elapsed # 0.0060.000.006 system.time(wide - reshape(x2[, -4], v.names = outcome, idvar = id, timevar = variable, direction = wide)) #user system elapsed # 0.169 0.000 0.169 system.time({ sel - unique(x2$variable) id - unique(x2$id) X - matrix(NA, ncol = length(sel) + 1, nrow = length(id)) X[, 1] - id colnames(X) - c('id', sel) r - mclapply(seq_along(sel), function(i){ out - x2[x2$variable == sel[i], ][, 3] }, mc.cores = 4) X[, -1] - do.call(rbind, r) X }) # user system elapsed # 0.125 0.011 0.074 wide2 - wide1 wide2$id - as.character(wide2$id) wide$id - as.character(wide$id) all.equal(wide, wide2, check.attributes=F) #[1] TRUE A.K. On Sunday, June 29, 2014 11:48 PM, Jorge I Velez jorgeivanve...@gmail.com wrote: Dear R-help, I am working with some data stored as filename.txt.gz in my working directory. After reading the data in using read.table(), I can see that each of them has four columns (variable, id, outcome, and rate) and the following structure: # sample data x2 - data.frame(variable = rep(paste0('x', 1:100), each = 100), id = rep(paste0('p', 1:100), 100), outcome = sample(0:2, 1, TRUE), rate = runif(1, 0.5, 1)) str(x2) Each variable, i.e., x1, x2,..., x100 is repeated as many times as the number of unique IDs (100 in this example). What I would like to do is to transform the data above in a long format. I can do this by using # reshape wide - reshape(x2[, -4], v.names = outcome, idvar = id, timevar = variable, direction = wide) str(wide) # or a hack with mclapply: require(parallel) sel - as.character(unique(x2$variable)) id - as.character(unique(x2$id)) X - matrix(NA, ncol = length(sel) + 1, nrow = length(id)) X[, 1] - id colnames(X) - c('id', sel) r - mclapply(seq_along(sel), function(i){ out - x2[x2$variable == sel[i], ][, 3] }, mc.cores = 4) X[, -1] - do.call(rbind, r) X However, I was wondering if it is possible to come up with another solution , hopefully faster than these . Unfortunately, either one of these takes a very long time to process, specially when the number of variables is very large ( 250,000) and the number of ids is ~2000. I would very much appreciate your suggestions. At the end of this message is my sessionInfo(). Thank you very much in advance. Best regards, Jorge Velez.- R sessionInfo() R version 3.0.2 Patched (2013-12-11 r64449) Platform: x86_64-apple-darwin10.8.0 (64-bit) locale: [1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8 attached base packages: [1] graphics grDevices utils datasets parallel compiler stats [8] methods base other attached packages: [1] knitr_1.6.3ggplot2_1.0.0 slidifyLibraries_0.3.1 [4] slidify_0.3.52 loaded via a namespace (and not attached): [1] colorspace_1.2-4 digest_0.6.4 evaluate_0.5.5 formatR_0.10 [5] grid_3.0.2 gtable_0.1.2 markdown_0.7.1 MASS_7.3-33 [9] munsell_0.4.2plyr_1.8.1 proto_0.3-10 Rcpp_0.11.2 [13] reshape2_1.4 scales_0.2.4 stringr_0.6.2tools_3.0.2 [17] whisker_0.4 yaml_2.1.13 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https
[R] From long to wide format
Dear R-help, I am working with some data stored as filename.txt.gz in my working directory. After reading the data in using read.table(), I can see that each of them has four columns (variable, id, outcome, and rate) and the following structure: # sample data x2 - data.frame(variable = rep(paste0('x', 1:100), each = 100), id = rep(paste0('p', 1:100), 100), outcome = sample(0:2, 1, TRUE), rate = runif(1, 0.5, 1)) str(x2) Each variable, i.e., x1, x2,..., x100 is repeated as many times as the number of unique IDs (100 in this example). What I would like to do is to transform the data above in a long format. I can do this by using # reshape wide - reshape(x2[, -4], v.names = outcome, idvar = id, timevar = variable, direction = wide) str(wide) # or a hack with mclapply: require(parallel) sel - as.character(unique(x2$variable)) id - as.character(unique(x2$id)) X - matrix(NA, ncol = length(sel) + 1, nrow = length(id)) X[, 1] - id colnames(X) - c('id', sel) r - mclapply(seq_along(sel), function(i){ out - x2[x2$variable == sel[i], ][, 3] }, mc.cores = 4) X[, -1] - do.call(rbind, r) X However, I was wondering if it is possible to come up with another solution , hopefully faster than these . Unfortunately, either one of these takes a very long time to process, specially when the number of variables is very large ( 250,000) and the number of ids is ~2000. I would very much appreciate your suggestions. At the end of this message is my sessionInfo(). Thank you very much in advance. Best regards, Jorge Velez.- R sessionInfo() R version 3.0.2 Patched (2013-12-11 r64449) Platform: x86_64-apple-darwin10.8.0 (64-bit) locale: [1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8 attached base packages: [1] graphics grDevices utils datasets parallel compiler stats [8] methods base other attached packages: [1] knitr_1.6.3ggplot2_1.0.0 slidifyLibraries_0.3.1 [4] slidify_0.3.52 loaded via a namespace (and not attached): [1] colorspace_1.2-4 digest_0.6.4 evaluate_0.5.5 formatR_0.10 [5] grid_3.0.2 gtable_0.1.2 markdown_0.7.1 MASS_7.3-33 [9] munsell_0.4.2plyr_1.8.1 proto_0.3-10 Rcpp_0.11.2 [13] reshape2_1.4 scales_0.2.4 stringr_0.6.2tools_3.0.2 [17] whisker_0.4 yaml_2.1.13 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] counting the number of rows that satisfy a certain criteria
Hi Kate, You could try sum(X[, 1] == 1 X[, 2] == 1) where X is your data set. HTH, Jorge.- On Sun, Jun 22, 2014 at 12:57 AM, Kate Ignatius kate.ignat...@gmail.com wrote: I have 4 columns, and about 300K plus rows with 0s and 1s. I'm trying to count how many rows satisfy a certain criteria... for instance, how many rows are there that have the first column == 1 as well as the second column == 1. I've tried using rowSums and colSums but it keeps giving me this type of error: Error in rowSums(X[1] == 1 X[2] == 1) : 'x' must be an array of at least two dimensions Thanks in advance! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R-es] pregunta
Hola Jose, Me funciona perfectamente: install.packages('ergm') #--- Please select a CRAN mirror for use in this session --- # also installing the dependencies 'statnet.common', 'trust' #snipped require(ergm) #snipped sessionInfo() #R version 3.0.2 Patched (2013-12-11 r64449) #Platform: x86_64-apple-darwin10.8.0 (64-bit) #locale: #[1] en_AU.UTF-8/en_AU.UTF-8/en_AU.UTF-8/C/en_AU.UTF-8/en_AU.UTF-8 #attached base packages: #[1] stats graphics grDevices utils datasets methods base #other attached packages: #[1] ergm_3.1.2 network_1.9.0statnet.common_3.1.1 #loaded via a namespace (and not attached): #[1] coda_0.16-1 DEoptimR_1.0-1grid_3.0.2lattice_0.20-29 #[5] Matrix_1.1-3 robustbase_0.91-1 tools_3.0.2 trust_0.1-6 Cual es tu sessionInfo()? Saludos, Jorge.- On Sun, Jun 15, 2014 at 9:14 PM, Dr. José A Betancourt Bethencourt jbetanco...@iscmc.cmw.sld.cu wrote: Estimados Tengo instalado el R3.03 Al instalar el paquete ergm me da el error : Error : objects '.lm.fit', 'confint.lm', 'dummy.coef.lm' are not exported by 'namespace:stats' ¿Cómo se puede corregir? instalción Loading required package: statnet.common Loading required package: network network: Classes for Relational Data Version 1.9.0 created on 2014-01-03. copyright (c) 2005, Carter T. Butts, University of California-Irvine Mark S. Handcock, University of California -- Los Angeles David R. Hunter, Penn State University Martina Morris, University of Washington Skye Bender-deMoll, University of Washington For citation information, type citation(network). Type help(network-package) to get started. Error : objects '.lm.fit', 'confint.lm', 'dummy.coef.lm' are not exported by 'namespace:stats' In addition: Warning messages: 1: package 'ergm' was built under R version 3.1.0 2: package 'statnet.common' was built under R version 3.1.0 3: package 'network' was built under R version 3.1.0 Error: package or namespace load failed for 'ergm' -- Nunca digas nunca, di mejor: gracias, permiso, disculpe. Este mensaje le ha llegado mediante el servicio de correo electronico que ofrece Infomed para respaldar el cumplimiento de las misiones del Sistema Nacional de Salud. La persona que envia este correo asume el compromiso de usar el servicio a tales fines y cumplir con las regulaciones establecidas Infomed: http://www.sld.cu/ [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es [[alternative HTML version deleted]] ___ R-help-es mailing list R-help-es@r-project.org https://stat.ethz.ch/mailman/listinfo/r-help-es
Re: [R] barp {plotrix} Start bars at 0 with a vector of positive values
Hi Pascal, Perhaps I am missing something, but what about changing passing ylim = c(0, 10) to barp()? Best, Jorge.- On Fri, Jun 13, 2014 at 7:50 PM, Pascal Oettli kri...@ymail.com wrote: Dear list, Please consider the following example: library(plotrix) barp(c(2,3,4,5,6,7,8), ylim=c(-10,10)) How to force the bars to start at 0? I could not find the way to do it. Regards, Pascal __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to print something in the same location in console?
Dear Juan, Perhaps the last example in http://stat.ethz.ch/R-manual/R-devel/library/utils/html/txtProgressBar.html is what you are looking for. Best, Jorge.- On Thu, Jun 12, 2014 at 8:49 PM, Juan Andres Hernandez jhernandezcabr...@gmail.com wrote: Hi I need to print the iteration number of a procedure but in the same location in console. Using cat with or without fill argument does not produce the desired outcome. Does anybody know how to get it?. for(i in 1:10) cat('Iteration:',i,fill=T) Thank's in advance Juan A. Hernandez [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Problem with rbind.fill
Hi Bill, You need require(plyr) ?rbind.fill and then the rest of the code you already tried. Best, Jorge.- On Mon, Jun 2, 2014 at 3:49 AM, Bill Bentley valuetr...@gmail.com wrote: The following works as it should... both-rbind(females,males) both workshop gender q1 q2 q3 q4 11 f 1 1 5 1 22 f 2 1 4 1 31 f 2 2 4 3 51 m 4 5 2 4 62 m 5 4 5 5 82 m 4 5 5 5 Next I changed the objects males and females so they had different numbers of variables and used rbind again and got an error which I expected. both - rbind(females, males) Error in rbind(deparse.level, ...) : numbers of columns of arguments do not match Next I attached the 'reshape' library and tried to use rbind.fill but as the code below shows, it does NOT work. The library seems to load ok (no error message) and appears in the list when I use the library() command. library(reshape) both - rbind.fill(females, males) Error: could not find function rbind.fill The book I'm following does this the same way and it works for them. I've re-downloaded and installed the reshape package but to no avail. Not sure what to do. Can't find an answer in help. I'm a brand new R user. Any suggestions what I'm doing wrong? Thanks! [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Problem with products in R ?
Try options(digits = 22) 168988580159 * 36662978 # [1] 6195624596620653568 HTH, Jorge.- On Sun, May 4, 2014 at 10:44 PM, ARTENTOR Diego Tentor diegotento...@gmail.com wrote: Trying algorithm for products with large numbers i encountered a difference between result of 168988580159 * 36662978 in my algorithm and r product. The Microsoft calculator confirm my number. Thanks. -- *Gráfica ARTENTOR * de Diego L. Tentor Echagüe 558 Tel.:0343 4310119 Paraná - Entre Ríos __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Return TRUE only for first match of values between matrix and vector.
Hi Nevil, Try apply(A, 2, function(x) x == B) HTH, Jorge.- On Fri, May 2, 2014 at 6:46 PM, nevil amos nevil.a...@gmail.com wrote: I wish to return True in a matrix for only the first match of a value per row where the value equals that in a vector with the same number of values as rosw in the matrix eg: A-matrix(c(2,3,2,1,1,2,NA,NA,NA,5,1,0,5,5,5),5,3) B-c(2,1,NA,1,5) desired result: [,1] [,2] [,3] [1,] TRUE FALSE FALSE [2,] FALSE NA FALSE [3,]NA NANA [4,] TRUE NA FALSE [5,] FALSE TRUE FALSE however A==B returns: [,1] [,2] [,3] [1,] TRUE TRUE FALSE [2,] FALSE NA FALSE [3,]NA NANA [4,] TRUE NA FALSE [5,] FALSE TRUE TRUE and apply(A,1,function(x) match (B,x)) returns [,1] [,2] [,3] [,4] [,5] [1,]1 NA1 NA NA [2,]3 NA NA11 [3,] NA222 NA [4,]3 NA NA11 [5,] NA NA332 thanks [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] how to extract a part of objects from list ?
Hi Kristi, Try out1970$smoot HTH, Jorge.- On Fri, May 2, 2014 at 10:00 AM, Kristi Glover kristi.glo...@hotmail.comwrote: Hi R User, I am wonedring how I can extract a part of objects from list. For example str(out1970) List of 8 $ comm : num [1:16, 1:57] 1 1 1 1 1 1 1 1 1 1 ... ..- attr(*, dimnames)=List of 2 .. ..$ : chr [1:16] H_s5 H_s1 R_s2 H_s2 ... .. ..$ : chr [1:57] Pimephales.promelas Semotilus.atromaculatus Rhinichthys.atratulus Pimephales.notatus ... $ u: num [1:16, 1:57] 0 0 0 0 0 0 0 0 0 0 ... ..- attr(*, dimnames)=List of 2 .. ..$ : chr [1:16] H_s5 H_s1 R_s2 H_s2 ... .. ..$ : chr [1:57] Pimephales.promelas Semotilus.atromaculatus Rhinichthys.atratulus Pimephales.notatus ... $ r: Named num [1:16] 0.0312 0.0938 0.1562 0.2188 0.2812 ... ..- attr(*, names)= chr [1:16] H_s5 H_s1 R_s2 H_s2 ... $ c: Named num [1:57] 0.00877 0.02632 0.04386 0.0614 0.07895 ... ..- attr(*, names)= chr [1:57] Pimephales.promelas Semotilus.atromaculatus Rhinichthys.atratulus Pimephales.notatus ... $ p: num 1.59 $ fill : num 0.294 $ statistic: num 11.4 $ smooth :List of 2 ..$ x: num [1:51] 0 0.02 0.04 0.06 0.08 0.1 0.12 0.14 0.16 0.18 ... ..$ y: num [1:51] 1 0.887 0.826 0.776 0.732 ... - attr(*, class)= chr nestedtemp I wanted to extract the value $ x and $y from List of 2 (smooth), but I could not extarct it. I used following functions sapply(out1970$smooth, [[,1) x y 0 1 sapply(out1970$smooth, [[,2) x y 0.020 0.8869147 But when I tried to extarct all value using sapply(out1970$smooth, [[,1:51) Error in FUN(X[[1L]], ...) : attempt to select more than one element would you mind to give some suggestions in how I get the value for all (1:51) ? thanks KG [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Faster way to transform vector [3 8 4 6 1 5] to [2 6 3 5 1 4]
Hi Xueming, Try (1:length(bo))[rank(bo)] In a function the above would be f - function(x){ N - length(x) (1:N)[rank(x)] } f(bo) # [1] 2 6 3 5 1 4 HTH, Jorge.- On Sat, Apr 26, 2014 at 7:54 PM, xmliu1...@gmail.com xmliu1...@gmail.comwrote: Hi, could anybody help me to find a fast way to fix the following question? Given a verctor of length N, for example bo = [3 8 4 6 1 5], I want to drive a vector whose elements are 1, 2, ..., N and the order of elements is the same as that in verctor bo. In this example, the result is supposed to be bt = [2 6 3 5 1 4]. I used the following code to solove this: bo - c(3, 8, 4, 6, 1, 5) N - length(bo) bt - rep(0, N) M - max(bo) temp - bo for(i in 1 : N) { min - M i_min - 0 for(j in 1 : N) { if(min = temp[j]) { min - temp[j] i_min -j } } bt[i_min] - i temp[i_min] - M+ 1 } bt [1] 2 6 3 5 1 4 However, the time complexity is O(N2). When N is larger than 100, it takes too much time. Is there any faster way to fix it? best Xueming xmliu1...@gmail.com [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] for loop to list files
Hi Beatriz, Try paste(val_mapped_petpe_, 1976:1981, 01.txt, sep=) Best, Jorge.- On Mon, Apr 21, 2014 at 6:43 PM, Beatriz R. Gonzalez Dominguez aguitatie...@hotmail.com wrote: Dear all, I'm trying to create a loop to select a series of files into my computer but I haven't been successful until now. I've looked into different possibilities but none has worked. I'd appretiate if you could help me by providing me with some ideas. Basically what I'd like to do is to create a character string variable [1:5], same as the one that could be obtained with 'list.files', but using a 'for' loop. This is one of the things I've tried but obviously doesn't yield the results I would like: for(i in 1976:1981){ PE.files_01_7681 - paste(val_mapped_petpe_, i, 01.txt, sep=) paste(PE.files_01_7681[i]) } Many thanks! __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/ posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Selecting numbers not divisible by 3
Hi there, Try X[X %% 3 == 0] HTH, Jorge.- On Thu, Mar 27, 2014 at 6:46 PM, Prabhakar Ghorpade dr.prabhaka...@gmail.com wrote: Hi, here's my code X - 1:100 I want to select number divisible by 3 out of them how can I select it? ( I tried following X - 1:100 DIV - Y - X/3 But I am getting whole number and number with fractions. WHole intgers are my number of interest from original X. How can I traceback to number divisbile by 3. ?) Thanks Kind Regards, Prabhakar -- Dr.Ghorpade Prabhakar B. Ph.D. Scholar ( Animal Biochemistry) Indian Veterinary Research Institute. India [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Calculations with aggregate data: confidence intervals
Hi Luigi, Thanks for sending the data in reproducible format. Perhaps something like this? aggregate(my.data[,3], list(my.data[,2]), FUN = function(x) t.test(x)$ conf.int[1:2]) #Group.1 x.1 x.2 #1 Unstimulated 5.296492e+02 2.410510e+03 #2ESAT6 9.105338e+00 4.078439e+03 #3CFP10 7.926311e+02 1.142267e+04 #4 Rv3615c 2.292658e+02 3.049608e+03 #5 Rv2654 7.396946e+02 7.311250e+03 #6 Rv3879 -6.603980e+02 5.777805e+03 #7 Rv3873 1.020891e+02 6.975473e+03 #8 PHA -2.236601e+05 6.508877e+05 HTH, Jorge.- On Tue, Mar 25, 2014 at 7:51 PM, Luigi Marongiu marongiu.lu...@gmail.comwrote: Dear all, I would like to calculate the confidence intervals on aggregate data. I know how to do this using the t test, but it did not work together with the aggregate function. Is there a function that can be applied to the aggregate function to obtain the (95%) confidence intervals, rather than applying a calculation? Best regards, Luigi my.data-structure(list( column_1 = 1:120, column_2 = structure(c( 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8, 1,2,3,4,5,6,7,8), .Label = c(Unstimulated, ESAT6, CFP10, Rv3615c, Rv2654, Rv3879, Rv3873, PHA), class = factor), column_3 = c( 192.0519108,183.6403531,53.46798757 ,83.60638077,69.60749873,159.4706861,256.8765622,499.2899303, 2170.799076,1411.349719,2759.472348,2098.973397,2164.739515 ,1288.676574,1611.486543,6205.229575, 870.7424981 ,465.9967135,191.8962375,864.0937485,2962.693675,1289.259137,2418.651212,7345.712517, 0,168.1198893,674.4342961,101.1575401,47.81596237,0,0,1420.793922, 142.6871331,5.466468742,291.9564635,80.73914133 ,73.02239621,64.47806871,144.3543635,3167.959757, 3164.748333 ,1092.634557,28733.20269,1207.87783,729.6090973,151.8706088,241.2466141,9600.963594, 1411.718287,12569.96285,1143.254476,6317.378481 ,16542.27718,79.68025792,1958.495138,7224.503437, 208.4382941 ,69.48609769,656.691151,0.499017582,7114.910926,187.6296174,41.73980805,8930.784541, 4.276752185,0.432300363,60.89228665 ,1.103924786,0.490686366,1.812993239,7.264531581,1518.610307, 2172.051528 ,595.8513744,17141.84336,589.6565971,1340.287628,117.350942,593.7034054,24043.61463, 0,81.83292179 ,1539.864321,36.41722958,8.385131047,161.7647376,65.21615696,7265.573875, 97.84753179 ,154.051827,0.613835842,10.06138851,45.04879285,176.8284258,18795.75462,3067686.769, 5780.34957,944.2200834,2398.235596,1083.393165,2541.714557,1251.670895,1547.178549,1792.679176, 3067.988416,8117.210173,23676.02226,8251.937547,17360.80494,18563.61561,16941.865,31453.96708, 2767.493803,4796.33016,12292.93705,3864.657567,9380.673835,14886.44683,8457.88646,26050.47191)), .Names = c(row, stimulation, copy), row.names = c(NA, -120L), class = data.frame) attach(my.data) # ??? question: confidence intervals for each variable ??? [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] plotting vectors of different lengths
Hi Eliza, Perhaps the following? matpoints(t(dat), type = 'l') HTH, Jorge.- On Sat, Mar 22, 2014 at 10:18 PM, eliza botto eliza_bo...@hotmail.comwrote: Dear useRs, I have two column vectors of different lengths say x=1,2,3,4,5,6,7,8 and y=1,2,3,4,5. I wanted to plot them by using points() command over an already existed image but got an error, Error in xy.coords(x, y) : 'x' and 'y' lengths differ.What i actually wanted to do was to plot the points in the following format. dat - read.table(text= 1 2 3 4 5 6 7 8 1 (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) 2 (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) 3 (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) 4 (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) 5 (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) ,sep=,header=TRUE,stringsAsFactors=FALSE) How can i do it? Thankyou very much indeed in advance. Eliza [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] plotting vectors of different lengths
You are welcome, Eliza. If I understand correctly, the following will do: x - 1:8 y - 1:5 matrix(apply(expand.grid(x = y, y = x), 1, function(r) paste0((, r[1], ,, r[2], ))), ncol = length(x)) Best, Jorge.- On Sat, Mar 22, 2014 at 10:37 PM, eliza botto eliza_bo...@hotmail.comwrote: Thankyou very much jorge. It would a great favor if i may know how to go from x=1,2,3,4,5,6,7,8 and y=1,2,3,4,5 TO 1 2 3 4 5 6 7 8 1 (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) 2 (2,1) (2,2) (2,3) (2,4) (2,5) (2,6) (2,7) (2,8) 3 (3,1) (3,2) (3,3) (3,4) (3,5) (3,6) (3,7) (3,8) 4 (4,1) (4,2) (4,3) (4,4) (4,5) (4,6) (4,7) (4,8) 5 (5,1) (5,2) (5,3) (5,4) (5,5) (5,6) (5,7) (5,8) Thnakyou very in advance Eliza From: jorgeivanve...@gmail.com Date: Sat, 22 Mar 2014 22:26:01 +1100 Subject: Re: [R] plotting vectors of different lengths To: eliza_bo...@hotmail.com CC: r-help@r-project.org Hi Eliza, Perhaps the following? matpoints(t(dat), type = 'l') HTH, Jorge.- On Sat, Mar 22, 2014 at 10:18 PM, eliza botto eliza_bo...@hotmail.comwrote: Dear useRs, I have two column vectors of different lengths say x=1,2,3,4,5,6,7,8 and y=1,2,3,4,5. I wanted to plot them by using points() command over an already existed image but got an error, Error in xy.coords(x, y) : 'x' and 'y' lengths differ.What i actually wanted to do was to plot the points in the following format. dat - read.table(text= 1 2 3 4 5 6 7 8 1 (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) 2 (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) 3 (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) 4 (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) 5 (1,1) (1,2) (1,3) (1,4) (1,5) (1,6) (1,7) (1,8) ,sep=,header=TRUE,stringsAsFactors=FALSE) How can i do it? Thankyou very much indeed in advance. Eliza [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] replace duplicates with 0
Hi Catalin, The following should give you some ideas: set.seed(123) x - rpois(50, 2) x idx - duplicated(x) x[idx] - 0 x Best, Jorge.- On Thu, Mar 13, 2014 at 11:35 PM, catalin roibu catalinro...@gmail.comwrote: Dear all! Is there a possibility to replace all duplicates values in data frame with 0? Thank you very much! -- --- Catalin-Constantin ROIBU Lecturer PhD, Forestry engineer Forestry Faculty of Suceava Str. Universitatii no. 13, Suceava, 720229, Romania office phone +4 0230 52 29 78, ext. 531 mobile phone +4 0745 53 18 01 +4 0766 71 76 58 FAX:+4 0230 52 16 64 silvic.usv.ro [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] dim vector or data.frame
Hi Berry, What about using NROW(input) ? Best, Jorge.- On Sat, Feb 15, 2014 at 2:26 AM, Berry Boessenkool berryboessenk...@hotmail.com wrote: Hi, In my function, I want to allow input to be a vector or a data.frame. Certain operations need to be done if the length or nrows exceeds one, but since nrow doesn't work for vectors, I cannot simply use if( nrow(input)1 | length(input)1 ) ... So is there a more elegant way to do this then with the following code? if( if(is.vector(input)) length(input)1 else nrow(input)1 ) ... thanks ahead, Berry __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] impossible to install package
Dear Eve, See http://cran.r-project.org/web/packages/languageR/index.html The name of the package is languageR, not LanguageR. Best, Jorge.- On Thu, Feb 13, 2014 at 3:39 PM, Eve Dupierrix evedupier...@gmail.comwrote: Hi, I want to install languageR but is doesn't work. I tried it by two ways: (1) by writing the following command but there is a warning message in response: install.packages (LanguageR) Warning message: package 'LanguageR' is not available (for R version 2.15.2) This problem appeared some weeks ago, but I was still able to install the package with the R package Installer with CRAN (binaries). (2) with the R package Installer which does not work since today. I tried different packages repository but it does not find LanguageR when I click on get list. The list remains empty. Could you please help me? Many thanks, Eve -- Eve Dupierrix Marie-Curie Research Fellow Queensland Brain Institute The University of Queensland St Lucia 4072 QLD Australia phone: +61 7 3346 3305 http://www.qbi.uq.edu.au/group-leader-mattingley http://webu2.upmf-grenoble.fr/LPNC/membre_eve_dupierrix [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Label point with (x,hat(y))
Try R plot(1:10) R text(1,3, expression((x, *hat(y)*)), pos=3) Best, Jorge.- On Sun, Nov 24, 2013 at 10:51 AM, David Arnold dwarnol...@suddenlink.netwrote: Hi, I'd like to do this: text(1,3,(x,yhat),pos=3) But using (x,hat(y)). Any suggestions? D. -- View this message in context: http://r.789695.n4.nabble.com/Label-point-with-x-hat-y-tp4681049.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] left transpose
Dear Dr. Vokey, Here is one approach, although may not be the more efficient: x - matrix(1:8, ncol = 4) x # [,1] [,2] [,3] [,4] #[1,]1357 #[2,]2468 t(x[, ncol(x):1]) # [,1] [,2] #[1,]78 #[2,]56 #[3,]34 #[4,]12 leftTranspose - function(x) t(x[, ncol(x):1]) leftTranspose(x) HTH. Jorge.- On Tue, Oct 22, 2013 at 12:52 PM, Vokey, John wrote: useRs, I frequently require the following transform of a matrix that I call a leftTranspose: -- transposes x such that the last items of each row become -- the first items in each column. E.g., -- a b c d -- e f g h -- becomes: -- d h -- c g -- b f -- a e because it is a leftward rotation. I have written my own function, but I was wondering whether I was reinventing the wheel here. Does such a transpose already exist in R (or matlab/octave/FreeMat, for that matter)? -- Please avoid sending me Word or PowerPoint attachments. See http://www.gnu.org/philosophy/no-word-attachments.html -Dr. John R. Vokey __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] replace Na values with the mean of the column which contains them
Consider the following: f - function(x){ m - mean(x, na.rm = TRUE) x[is.na(x)] - m x } apply(de, 2, f) HTH, Jorge.- On Tue, Jul 30, 2013 at 2:39 AM, iza.ch1 iza@op.pl wrote: Hi everyone I have a problem with replacing the NA values with the mean of the column which contains them. If I replace Na with the means of the rest values in the column, the mean of the whole column will be still the same as if I would have omitted NA values. I have the following data de [,1][,2] [,3] [1,] NA -0.26928087 -0.1192078 [2,] NA 1.20925752 0.9325334 [3,] NA 0.38012008 -1.8927164 [4,] NA -0.41778861 1.4330507 [5,] NA -0.49677462 0.2892706 [6,] NA -0.13248754 1.3976522 [7,] NA -0.54179054 0.2295291 [8,] NA 0.35788624 -0.5009389 [9,] 0.27500571 -0.41467591 -0.3426560 [10,] -3.07568579 -0.59234248 -0.8439027 [11,] -0.42240954 0.73642396 -0.4971999 [12,] -0.26901731 -0.06768044 -1.6127122 [13,] 0.01766284 -0.40321968 -0.6508823 [14,] -0.80999580 -1.52283305 1.4729576 [15,] 0.20805934 0.25974308 -1.6093478 [16,] 0.03036708 -0.04013730 0.1686006 and I wrote the code de[which(is.na(de))]-sapply(seq_len(ncol(de)),function(i) {mean(de[,i],na.rm=TRUE)}) I get as the result [,1][,2] [,3] [1,] -0.50575168 -0.26928087 -0.1192078 [2,] -0.1376 1.20925752 0.9325334 [3,] -0.13412312 0.38012008 -1.8927164 [4,] -0.50575168 -0.41778861 1.4330507 [5,] -0.1376 -0.49677462 0.2892706 [6,] -0.13412312 -0.13248754 1.3976522 [7,] -0.50575168 -0.54179054 0.2295291 [8,] -0.1376 0.35788624 -0.5009389 [9,] 0.27500571 -0.41467591 -0.3426560 [10,] -3.07568579 -0.59234248 -0.8439027 [11,] -0.42240954 0.73642396 -0.4971999 [12,] -0.26901731 -0.06768044 -1.6127122 [13,] 0.01766284 -0.40321968 -0.6508823 [14,] -0.80999580 -1.52283305 1.4729576 [15,] 0.20805934 0.25974308 -1.6093478 [16,] 0.03036708 -0.04013730 0.1686006 It has replaced the NA values in first column with mean of first column -0.505... and second cell with mean of second column etc. I want to have the result like this: [,1][,2] [,3] [1,] -0.50575168 -0.26928087 -0.1192078 [2,] -0.50575168 1.20925752 0.9325334 [3,] -0.50575168 0.38012008 -1.8927164 [4,] -0.50575168 -0.41778861 1.4330507 [5,] -0.50575168 -0.49677462 0.2892706 [6,] -0.50575168 -0.13248754 1.3976522 [7,] -0.50575168 -0.54179054 0.2295291 [8,] -0.50575168 0.35788624 -0.5009389 [9,] 0.27500571 -0.41467591 -0.3426560 [10,] -3.07568579 -0.59234248 -0.8439027 [11,] -0.42240954 0.73642396 -0.4971999 [12,] -0.26901731 -0.06768044 -1.6127122 [13,] 0.01766284 -0.40321968 -0.6508823 [14,] -0.80999580 -1.52283305 1.4729576 [15,] 0.20805934 0.25974308 -1.6093478 [16,] 0.03036708 -0.04013730 0.1686006 Thanks in advance __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] find closest value in a vector based on another vector values
Dear Andras, Try a[findInterval(b, a)] [1] 8 32 HTH, Jorge.- On Tue, Jun 18, 2013 at 10:34 PM, Andras Farkas motyoc...@yahoo.com wrote: Dear All, would you please provide your thoughts on the following: let us say I have: a -c(1,5,8,15,32,69) b -c(8.5,33) and I would like to extract from a the two values that are closest to the values in b, where the length of this vectors may change but b will allways be shorter than a. So at the end based on this example I should have the result f as f -c(8,32) appreciate the help, Andras __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] find closest value in a vector based on another vector values
T hank you very much, Bert. Point well taken. Regards, Jorge.- On Wed, Jun 19, 2013 at 12:07 AM, Bert Gunter gunter.ber...@gene.comwrote: Jorge: No. a -c(1,5,8,15,32,33.5,69) b -c(8.5,33) a[findInterval(b, a)] [1] 8 32 ##should be 8 33.5 I believe it has to be done explicitly by finding all the differences and choosing those n with minimum values, depending on what n you want. Note that the problem is incompletely specified. What if the same value of a is closest to several values of b? -- do you want all the values you choose to be different or not, in which case they may not be minimum? a - c(1, 8, 9) b - c(2,3) Then what are the 2 closest values of a to b? -- Bert On Tue, Jun 18, 2013 at 5:43 AM, Jorge I Velez jorgeivanve...@gmail.com wrote: Dear Andras, Try a[findInterval(b, a)] [1] 8 32 HTH, Jorge.- On Tue, Jun 18, 2013 at 10:34 PM, Andras Farkas motyoc...@yahoo.com wrote: Dear All, would you please provide your thoughts on the following: let us say I have: a -c(1,5,8,15,32,69) b -c(8.5,33) and I would like to extract from a the two values that are closest to the values in b, where the length of this vectors may change but b will allways be shorter than a. So at the end based on this example I should have the result f as f -c(8,32) appreciate the help, Andras __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How can we access an element in a structure
Hi Miao, Try attributes(test1)[[1]] HTH, Jorge.- On Tue, Jun 11, 2013 at 3:49 PM, jpm miao miao...@gmail.com wrote: Hi, I have a structure, which is the result of a function How can I access the elements in the gradient? dput(test1) structure(-1.17782911684913, gradient = structure(c(-0.0571065371783791, -0.144708170683529), .Dim = 1:2, .Dimnames = list(NULL, c(x1, x2 test1[[1]] [1] -1.177829 test1 [1] -1.177829 attr(,gradient) x1 x2 [1,] -0.05710654 -0.1447082 test1[gradient] [1] NA Thanks, Miao [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] boot, what am I doing wrong?
Hi there, You need a function for your statistic: boot(x, function(x, index) mean(x[index]), R = 1000) ORDINARY NONPARAMETRIC BOOTSTRAP Call: boot(data = x, statistic = function(x, index) mean(x[index]), R = 1000) Bootstrap Statistics : original biasstd. error t1* 0.4069613 0.005465687 0.07355997 See http://www.mayin.org/ajayshah/KB/R/documents/boot.html for more information and examples. HTH, Jorge.- On Fri, Jun 7, 2013 at 6:03 PM, Rguy r...@123mail.org wrote: I am getting started with the boot package and boot command. As a first step I tried the following. Something is wrong, but i can't see what. Any advice would be much appreciated. x = runif(10) mean(x) [1] 0.467626212374307 boot(x, mean, R=100) Error in mean.default(data, original, ...) : 'trim' must be numeric of length one Enter a frame number, or 0 to exit 1: boot(x, mean, R = 100) 2: statistic(data, original, ...) 3: mean.default(data, original, ...) 4: stop('trim' must be numeric of length one) 5: (function () { utils::recover() })() When I enter a trim parameter I get the following: boot(x, mean, R=100, trim=0) [...some output omitted...] Warning in if (na.rm) x - x[!is.na(x)] : the condition has length 1 and only the first element will be used Warning in if (na.rm) x - x[!is.na(x)] : the condition has length 1 and only the first element will be used Warning in if (na.rm) x - x[!is.na(x)] : the condition has length 1 and only the first element will be used Warning in if (na.rm) x - x[!is.na(x)] : the condition has length 1 and only the first element will be used Warning in if (na.rm) x - x[!is.na(x)] : the condition has length 1 and only the first element will be used Warning in if (na.rm) x - x[!is.na(x)] : the condition has length 1 and only the first element will be used Warning in if (na.rm) x - x[!is.na(x)] : the condition has length 1 and only the first element will be used Warning in if (na.rm) x - x[!is.na(x)] : the condition has length 1 and only the first element will be used ORDINARY NONPARAMETRIC BOOTSTRAP Call: boot(data = x, statistic = mean, R = 100, trim = 0) Bootstrap Statistics : original biasstd. error t1* 0.467626212374307 0 0 When I enter an na.rm value as well the following is output: boot(x, mean, R=100, trim=0, na.rm=T) ORDINARY NONPARAMETRIC BOOTSTRAP Call: boot(data = x, statistic = mean, R = 100, trim = 0, na.rm = T) Bootstrap Statistics : original biasstd. error t1* 0.467626212374307 0 0 Notice that the standard error is 0, indicating that no bootstrapping has actually taken place. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Subsetting out missing values for a certain variable
Daniel, You need == instead of =. HTH, Jorge.- Sent from my phone. Please excuse my brevity and misspelling. On Jun 6, 2013, at 10:36 AM, Daniel Tucker dtuck...@u.rochester.edu wrote: Also tried this but results werent any different subset1- subset(dframe, glb_ind=Y | sample==1 | !is.na(glb_ind)) subset2-subset(dframe, cwar_ind=Y |sample==2 | !is.na(cwar_ind)) subset3-subset(dframe, reg_ind=Y | sample==3 | !is.na(reg_ind)) On Wed, Jun 5, 2013 at 9:33 AM, Daniel Tucker daniel.tuc...@rochester.eduwrote: I am trying to create a new datafarme using the subset function given 2 conditions subset1- subset(dframe, glb_ind=Y | sample==1) subset2-subset(dframe, cwar_ind=Y | sample==2) subset3-subset(dframe, reg_ind=Y | sample==3) However, my first conditions (glb_ind,cwar_ind, and reg_ind) all have missing values (they are either Y, N, or no value. In subsetting my data, I am looking to not only get rid of the N in the new dataframes, but also the NA's. I don't want to na.omit the entire data frame; I only want to get rid of missing values (and non Y values) for a certain variable (glb_ind, cwar_ind, reg_ind) for each subset. Is there anyway I can do this? Thanks, Dan [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Refer to Data Frame Name Inside a List
Try names(ResList) HTH, Jorge.- Sent from my phone. Please excuse my brevity and misspelling. On Jun 5, 2013, at 12:34 AM, Sparks, John James jspa...@uic.edu wrote: Dear R Helpers, I have a fairly complicated list of data frames. To give you an idea of the structure, the top of the str output is shown below. How do I refer to the data.frame name for each data.frame in the list? That is, how can I pull the terms Advertising2007, AirFreightDelivery2007, Apparel2007 etc. out of the list? I need them to keep track of correlations that I am doing inside each data frame of the list. Apologies for not sending a reproducible example. I am hoping that someone knows this off the top of their head. --John Sparks str(ResList) List of 60 $ Advertising2007 :'data.frame': 21 obs. of 10 variables: ..$ RFPred : num [1:21] -0.01749 -0.00801 -0.01155 -0.01494 -0.03715 ... ..$ marsPred : num [1:21] 0.0901 0.0127 0.0616 0.0618 -0.0559 ... ..$ GainRepAft3 : num [1:21] -0.0673 -0.0183 -0.2353 0.0294 -0.059 ... ..$ Industry : chr [1:21] Advertising2007 Advertising2007 Advertising2007 Advertising2007 ... ..$ dateavail: Factor w/ 346 levels 2008-02-01,2008-02-13,..: 18 4 14 12 13 19 1 15 17 8 ... ..$ FinYearEnd : Factor w/ 12 levels 2007-12-01,2007-03-01,..: 1 1 1 1 1 1 1 1 1 1 ... ..$ GainAft1Aft30: num [1:21] -0.2376 -0.1384 -0.1176 0.0145 0.0527 ... ..$ GainAft1Aft60: num [1:21] -0.36212 -0.17801 -0.23529 -0.00501 -0.27414 ... ..$ GainAft1Aft90: num [1:21] -0.516 -0.203 -0.176 0.024 -0.241 ... ..$ groups : Factor w/ 40 levels -0.04013239,..: 4 11 8 6 1 1 10 13 2 5 ... $ AirFreightDelivery2007 :'data.frame': 20 obs. of 10 variables: ..$ RFPred : num [1:20] 0.00322 -0.00351 0.034 0.01095 0.02237 ... ..$ marsPred : num [1:20] -0.013 -0.109 0.0662 0.0353 0.0662 ... ..$ GainRepAft3 : num [1:20] 0.0344 -0.0659 0.054 0.045 0.0266 ... ..$ Industry : chr [1:20] AirFreightDelivery2007 AirFreightDelivery2007 AirFreightDelivery2007 AirFreightDelivery2007 ... ..$ dateavail: Factor w/ 346 levels 2008-02-01,2008-02-13,..: 22 10 26 33 35 32 25 23 31 10 ... ..$ FinYearEnd : Factor w/ 12 levels 2007-12-01,2007-03-01,..: 2 1 1 1 1 1 1 3 1 1 ... ..$ GainAft1Aft30: num [1:20] -0.0656 -0.1539 -0.1002 -0.0694 -0.4101 ... ..$ GainAft1Aft60: num [1:20] -0.133 -0.141 -0.242 -0.691 -0.212 ... ..$ GainAft1Aft90: num [1:20] -0.0523 -0.0673 -0.1793 -0.6875 -0.187 ... ..$ groups : Factor w/ 40 levels -0.04013239,..: 24 16 39 32 37 21 17 30 35 37 ... $ Apparel2007 :'data.frame': 28 obs. of 10 variables: ..$ RFPred : num [1:28] 0.011439 0.021311 0.014564 0.018168 -0.000892 ... ..$ marsPred : num [1:28] -0.001463 0.0345 0.027227 -0.000129 -0.006483 ... __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Repeating sequence elements
Try rep(1:length(v), v) HTH, Jorge.- On Fri, May 17, 2013 at 8:53 PM, Stefan Petersson ste...@inizio.se wrote: I want to create a sequence, repeating each element according to a vector. I have this: v - c(4, 4, 4, 3, 3, 2) And want to create this: 1 1 1 1 2 2 2 2 3 3 3 3 4 4 4 5 5 5 6 6 TIA // s R version 3.0.0 (2013-04-03) Platform: x86_64-pc-linux-gnu (64-bit) locale: [1] LC_CTYPE=en_US.UTF-8 LC_NUMERIC=C [3] LC_TIME=en_US.UTF-8 LC_COLLATE=en_US.UTF-8 [5] LC_MONETARY=en_US.UTF-8 LC_MESSAGES=en_US.UTF-8 [7] LC_PAPER=C LC_NAME=C [9] LC_ADDRESS=C LC_TELEPHONE=C [11] LC_MEASUREMENT=en_US.UTF-8 LC_IDENTIFICATION=C attached base packages: [1] stats graphics grDevices utils datasets methods base [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How can I find negative items from a vector with a short command?
Check ?== and try f[f == 4] More in section 2.4 at http://cran.r-project.org/doc/manuals/R-intro.pdf On Wed, May 8, 2013 at 5:14 PM, jpm miao miao...@gmail.com wrote: Thanks, but why does f[f=4] yield 16 instead of 4? f [1] -24 -8 16 -32 64 -128 f[f0] [1] -2 -8 -32 -128 f[f0] [1] 4 16 64 f[f=4] [1] 16 2013/5/8 Jorge I Velez jorgeivanve...@gmail.com f [ f 0 ] On Wed, May 8, 2013 at 11:54 AM, jpm miao miao...@gmail.com wrote: Hi, I have a vector f with some negative columns. I remember that there is an easy expression that can find out negative items. Can someone tell me how I can do it? It seems to be f[i such that f[i]0 ...] Thanks, Miao d-1:7 f-(-2)^d f [1] -24 -8 16 -32 64 -128 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How can I find negative items from a vector with a short command?
f [ f 0 ] On Wed, May 8, 2013 at 11:54 AM, jpm miao miao...@gmail.com wrote: Hi, I have a vector f with some negative columns. I remember that there is an easy expression that can find out negative items. Can someone tell me how I can do it? It seems to be f[i such that f[i]0 ...] Thanks, Miao d-1:7 f-(-2)^d f [1] -24 -8 16 -32 64 -128 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How can I access the rowname of a data?
rownames(a) perhaps? HTH, Jorge.- On Mon, May 6, 2013 at 6:03 PM, jpm miao miao...@gmail.com wrote: Hi, Below is the output from an R package. The first column (4, 5, 6, 7, which is unnamed) is the company name (code), while the second column efficiency is the performance of each company, which is the primary output result. How can I access the first column, rowname, 4, 5, 6, 7, 8, 9, 11? How can I build a dataframe consisting of two columns, the company name (code) and the efficiency? as.data.frame(a) produce only a column with company names, where company names are not accessible. Thanks, Miao a efficiency 4 0.26902196 5 0.30967213 6 0.33841116 7 0.36174368 8 0.36415004 9 0.37977151 11 0.42136627 12 0.40270465 13 0.49386586 15 0.12549416 16 0.17616129 17 0.45815803 18 0.09380655 21 0.97223375 40 0.43234228 48 0.25185516 50 0.28382549 52 0.44653074 53 0.27899931 54 0.42288119 81 0.49108330 101 0.20489971 102 0.24817017 103 0.36290391 108 0.23457172 118 0.25237865 147 0.25926483 803 0.45134936 805 0.36135729 806 0.28311573 807 0.32053914 808 0.35295777 809 0.17983115 810 0.30132804 812 0.48614262 814 0.30770005 815 0.29402445 816 0.35863589 822 0.50282266 825 0.46845802 dput(a) structure(c(0.269021958172058, 0.30967212679043, 0.338411157000573, 0.361743675159821, 0.364150044117951, 0.37977150519361, 0.421366274731659, 0.402704652137264, 0.49386585901599, 0.125494157670888, 0.176161288740361, 0.458158033521881, 0.0938065485975032, 0.972233754473899, 0.432342283512805, 0.251855164210068, 0.283825485987841, 0.446530739128589, 0.278999313704237, 0.422881194129863, 0.491083297407732, 0.20489970948236, 0.248170174737282, 0.362903909816553, 0.234571723781862, 0.252378650048782, 0.259264826952972, 0.451349355848852, 0.361357286628178, 0.283115732632982, 0.32053914247248, 0.352957770657268, 0.179831147819322, 0.30132803776434, 0.486142623827358, 0.307700045108983, 0.29402444509927, 0.358635886286322, 0.502822662489642, 0.468458021786023), .Dim = c(40L, 1L), .Dimnames = list(c(4, 5, 6, 7, 8, 9, 11, 12, 13, 15, 16, 17, 18, 21, 40, 48, 50, 52, 53, 54, 81, 101, 102, 103, 108, 118, 147, 803, 805, 806, 807, 808, 809, 810, 812, 814, 815, 816, 822, 825 ), efficiency)) a1-as.data.frame(a) View(`a1`) a1$name NULL a1$names NULL a1$row.names NULL a1$efficiency [1] 0.26902196 0.30967213 0.33841116 0.36174368 [5] 0.36415004 0.37977151 0.42136627 0.40270465 [9] 0.49386586 0.12549416 0.17616129 0.45815803 [13] 0.09380655 0.97223375 0.43234228 0.25185516 [17] 0.28382549 0.44653074 0.27899931 0.42288119 [21] 0.49108330 0.20489971 0.24817017 0.36290391 [25] 0.23457172 0.25237865 0.25926483 0.45134936 [29] 0.36135729 0.28311573 0.32053914 0.35295777 [33] 0.17983115 0.30132804 0.48614262 0.30770005 [37] 0.29402445 0.35863589 0.50282266 0.46845802 a1 efficiency 4 0.26902196 5 0.30967213 6 0.33841116 7 0.36174368 8 0.36415004 9 0.37977151 11 0.42136627 12 0.40270465 13 0.49386586 15 0.12549416 16 0.17616129 17 0.45815803 18 0.09380655 21 0.97223375 40 0.43234228 48 0.25185516 50 0.28382549 52 0.44653074 53 0.27899931 54 0.42288119 81 0.49108330 101 0.20489971 102 0.24817017 103 0.36290391 108 0.23457172 118 0.25237865 147 0.25926483 803 0.45134936 805 0.36135729 806 0.28311573 807 0.32053914 808 0.35295777 809 0.17983115 810 0.30132804 812 0.48614262 814 0.30770005 815 0.29402445 816 0.35863589 822 0.50282266 825 0.46845802 View(`a1`) a1[1,] [1] 0.269022 a1[2,] [1] 0.3096721 [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] R does not subset
Hi Kasia, You need subset(REC2, INFECTION==Infected ) (note the space after Infected). HTH, Jorge.- On Fri, May 3, 2013 at 7:48 PM, Katarzyna Kulma katarzyna.ku...@gmail.comwrote: Hi everyone, I know there have been several requests regarding subsetting before, but none of them really helps with my problem: I'm trying to subset only infected individuals from the REC2 data.frame: str(REC2) 'data.frame':362 obs. of 7 variables: $ RINGNO : Factor w/ 370 levels BL17546,BL17577,..: 78 81 67 41 58 66 17 $ year : Factor w/ 8 levels Y2002,Y2003,..: 1 2 1 2 1 1 2 1 1 3 ... $ ccFLEDGE : int 6 6 6 5 6 7 6 7 6 5 ... $ rec2012 : int 2 1 2 2 1 2 1 1 1 0 ... $ binage : Factor w/ 2 levels ad,juv: 1 2 1 1 1 1 1 1 1 1 ... $ INFECTION: Factor w/ 2 levels Infected ,Uninfected : 2 1 2 1 2 2 1 2 2 1 ... $ all.rsLD : num -4.62 -6.19 -3.62 -4.19 -2.62 ... using either RECinf-REC2[which (REC2$INFECTION==Infected),] or RECinf-subset(REC2, INFECTION==Infected) in both cases I get empty data frame (0 observations): str(RECinf) 'data.frame':0 obs. of 7 variables: $ RINGNO : Factor w/ 370 levels BL17546,BL17577,..: $ year : Factor w/ 8 levels Y2002,Y2003,..: $ ccFLEDGE : int $ rec2012 : int $ binage : Factor w/ 2 levels ad,juv: $ INFECTION: Factor w/ 2 levels Infected ,Uninfected : $ all.rsLD : num When subsetting, R doesn't return any warning or error message. Besides, I used same codes many times before and they worked perfectly well. Any ideas why this case is different? Thanks for your help, Kasia [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Need help on matrix calculation
Christofer, The following should get you started: r - Mat[match(rownames(Mat), Subscript_Vec),] rownames(r) - Subscript_Vec r HTH, Jorge.- On Mon, Apr 29, 2013 at 11:38 PM, Christofer Bogaso bogaso.christo...@gmail.com wrote: Hello again, Let say I have 1 matrix: Mat - matrix(1:12, 4, 3) rownames(Mat) - letters[1:4] Now I want to subscript of Mat in following way: Subscript_Vec - c(a, e, b, c) However when I want to use this vector, I am geting following error: Mat[Subscript_Vec, ] Error: subscript out of bounds Basically I want to get my final matrix in following way: V1 V2 V3 a 1 5 9 e NA NA NA b 2 6 10 c 3 7 11 i.e. if some of the element(s) in 'Subscript_Vec' is not in 'Mat' then that row would be filled by NA, WITHOUT altering the sequence of 'Subscript_Vec' Is there any direct way to achieve that? Thanks and regards, __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Need help on matrix calculation
Sorry, the first line should have been Mat[match( Subscript_Vec, rownames(Mat)),] and the rest remains the same. Best, Jorge.- On Mon, Apr 29, 2013 at 11:45 PM, Jorge I Velez jorgeivanve...@gmail.comwrote: Christofer, The following should get you started: r - Mat[match(rownames(Mat), Subscript_Vec),] rownames(r) - Subscript_Vec r HTH, Jorge.- On Mon, Apr 29, 2013 at 11:38 PM, Christofer Bogaso bogaso.christo...@gmail.com wrote: Hello again, Let say I have 1 matrix: Mat - matrix(1:12, 4, 3) rownames(Mat) - letters[1:4] Now I want to subscript of Mat in following way: Subscript_Vec - c(a, e, b, c) However when I want to use this vector, I am geting following error: Mat[Subscript_Vec, ] Error: subscript out of bounds Basically I want to get my final matrix in following way: V1 V2 V3 a 1 5 9 e NA NA NA b 2 6 10 c 3 7 11 i.e. if some of the element(s) in 'Subscript_Vec' is not in 'Mat' then that row would be filled by NA, WITHOUT altering the sequence of 'Subscript_Vec' Is there any direct way to achieve that? Thanks and regards, __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Decomposing a List
Dear Dr. Harding, Try sapply(L, [, 1) sapply(L, [, 2) HTH, Jorge.- On Thu, Apr 25, 2013 at 8:16 PM, Ted Harding ted.hard...@wlandres.netwrote: Greetings! For some reason I am not managing to work out how to do this (in principle) simple task! As a result of applying strsplit() to a vector of character strings, I have a long list L (N elements), where each element is a vector of two character strings, like: L[1] = c(A1,B1) L[2] = c(A2,B2) L[3] = c(A3,B3) [etc.] From L, I wish to obtain (as directly as possible, e.g. avoiding a loop) two vectors each of length N where one contains the strings that are first in the pair, and the other contains the strings which are second, i.e. from L (as above) I would want to extract: V1 = c(A1,A2,A3,...) V2 = c(B1,B2,B3,...) Suggestions? With thanks, Ted. - E-Mail: (Ted Harding) ted.hard...@wlandres.net Date: 25-Apr-2013 Time: 11:16:46 This message was sent by XFMail __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Looking for a better code for my problem.
Try subset(Dat, AA == A | (AA == B BB == b)) HTH, Jorge.- On Wed, Apr 24, 2013 at 8:21 PM, Christofer Bogaso bogaso.christo...@gmail.com wrote: Hello again, Let say I have following data: Dat - structure(list(AA = structure(c(3L, 1L, 2L, 1L, 2L, 3L, 3L, 2L, 3L, 1L, 1L, 3L, 3L, 2L, 2L, 3L, 2L, 1L, 1L, 1L), .Label = c(A, B, C), class = factor), BB = structure(c(2L, 3L, 2L, 2L, 2L, 3L, 2L, 2L, 2L, 1L, 1L, 2L, 3L, 1L, 3L, 2L, 1L, 2L, 2L, 3L ), .Label = c(a, b, c), class = factor), CC = 1:20), .Names = c(AA, BB, CC), row.names = c(NA, -20L), class = data.frame) Now I want to select a subset of that 'Dat', for which: 1. First column will contain ALL A 2. First column will contain those B for which BB = b in second column. Therefore I tries following: Only_A - Dat[Dat[, 'AA'] == A, ] Only_B - Dat[Dat[, 'AA'] == B, ] rbind(Only_A, Only_B[Only_B[, 'BB'] == b, ]) AA BB CC 2 A c 2 4 A b 4 10 A a 10 11 A a 11 18 A b 18 19 A b 19 20 A c 20 3 B b 3 5 B b 5 8 B b 8 However I believe there must be some better code to achieve that which is tidier, i.e. there must be some 1-liner code. Can somebody suggest any better approach if possible? Thanks and regards, __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] subset dataframe
Mike, You need subset(agoa, agoa$X.1 == AGOA ) instead of subset(agoa, agoa$X.1 == AGOA) (note the space after the last A in AGOA. HTH, Jorge.- On Tue, Apr 23, 2013 at 7:14 AM, Mihai Nica mihain...@yahoo.com wrote: I can't understand what is happening. This is the code and results: agoa - read.table(file = C:/Users/HTPC/Documents/_Documents/Research/WithDidia/AGOAUSImports.txt, header = T, sep = \t, dec = ., na.strings = NA, stringsAsFactors = T)# str(agoa); names(agoa) 'data.frame':109 obs. of 19 variables: $ X: Factor w/ 39 levels Angola ,Benin ,..: 1 1 1 2 2 3 3 3 4 4 ... $ X.1 : Factor w/ 3 levels AGOA ,GSP ,..: 3 1 2 3 2 3 1 2 3 1 ... $ X1996: int 2687143 0 2 18084 70 23356 0 3624 3835 0 ... $ X1997: int 2427824 0 356492 4303 3437 18758 0 5882 930 0 ... $ X1998: int 1205545 0 1045996 1335 2269 14010 0 5660 503 0 ... $ X1999: int 1596052 0 828761 6042 11788 12071 0 4824 2695 0 ... $ X2000: int 2178246 0 1378777 1026 1414 38024 0 2922 502 0 ... $ X2001: int 464083 0 2635482 1108 178 19429 0 1221 4919 0 ... $ X2002: int 386118 0 2728387 680 0 25014 3707 871 2862 0 ... $ X2003: int 441647 0 3822701 602 0 7293 6343 0 788 0 ... $ X2004: int 471009 1349411 2700750 1310 215 52840 20119 7 474 0 ... $ X2005: int 1081143 3662774 3740324 509 4 148102 30044 7 1962 0 ... $ X2006: int 1670746 4127605 5920870 531 24 224382 27688 27 954 6 ... $ X2007: int 2346392 3898345 6262784 5076 0 155818 31331 304 1415 0 ... $ X2008: int 8151345 8119377 2639949 31010 0 202938 15803 104 495 0 ... $ X2009: int 5257573 3018965 1062246 425 16 119540 12362 8 2096 0 ... $ X2010: int 6542843 4741574 662450 271 4 158147 11559 8 2368 2 ... $ X2011: int 8423316 5174087 70 1957 14 276223 15479 1585 3599 2 ... $ X2012: int 8017601 1761068 45196 2625 49 204337 10427 1757 2233 5 ... [1] X X.1 X1996 X1997 X1998 X1999 X2000 X2001 X2002 [10] X2003 X2004 X2005 X2006 X2007 X2008 X2009 X2010 X2011 [19] X2012 agoa.AGOA - subset(agoa, agoa$X.1 == AGOA) str(agoa.AGOA) 'data.frame':0 obs. of 19 variables: $ X: Factor w/ 39 levels Angola ,Benin ,..: $ X.1 : Factor w/ 3 levels AGOA ,GSP ,..: $ X1996: int $ X1997: int $ X1998: int $ X1999: int $ X2000: int $ X2001: int $ X2002: int $ X2003: int $ X2004: int $ X2005: int $ X2006: int $ X2007: int $ X2008: int $ X2009: int $ X2010: int $ X2011: int $ X2012: int I did try : agoa.AGOA = agoa[X.1 == AGOA,] with similar results. All the help I looked over gives these as solutions... mike [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Subsetting a large number into smaller numbers and find the largest product
Dear Janesh, Here is one way: # note x is a character x - 73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557 k - nchar(x) # digits in x b - 5 # period tapply(strsplit(x, )[[1]], rep(1:(nchar(x)/b), each = b), function(x) prod(as.numeric(x))) HTH, Jorge.- On Thu, Apr 18, 2013 at 6:47 PM, Janesh Devkota janesh.devk...@gmail.comwrote: Hello, I have a big number lets say of around hundred digits. I want to subset that big number into consecutive number of 5 digits and find the product of those 5 digits. For example my first 5 digit number would be 73167. I need to check the product of the individual numbers in 73167 and so on. The sample number is as follows: 73167176531330624919225119674426574742355349194934969835203127745063262395783180169848018694788518438586156078911294949545950173795833195285320880551112540698747158523863050715693290963295227443043557 I have a problem subsetting the small numbers out of the big number. Any help is highly appreciated. Best Regards, Janesh Devkota [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Error: could not find function invlogit and bayesglm
Hi Carrington, You also need the boot package (see http://stat.ethz.ch/R-manual/R-patched/library/boot/html/inv.logit.html ) As for the other function, please load the arm package, e.g., require(arm) require(boot) and then you will be able to use the functions mentioned below. HTH, Jorge.- On Wed, Apr 17, 2013 at 6:08 PM, S'dumo Masango masan...@uniswa.sz wrote: I have installed the arm package and its dependents (e.g MATRIX, etc), but cannot use the functions invlogit and bayesglm because it gives me the error message Error: could not find function invlogit or Error: could not find function invlogit. What could be the problem. Regards Carrington [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] non linear equation
Dear Catalin, You can look at ?nls. Alternatively, you could also consider a linear model as follows, where d is your data: # plot your data with(d, plot(cls, proc, las = 1)) # linear model fit - lm(proc ~ I(1/cls) + I((1/cls)^2), data = d) summary(fit) # plotting with(d, plot(cls, proc, las = 1)) grid - seq(min(d$cls), max(d$cls), length = 1000) points(grid, predict(fit, data.frame(cls = grid)), type = l, col = 2) HTH, Jorge.- On Wed, Apr 10, 2013 at 7:01 PM, catalin roibu catalinro...@gmail.comwrote: Hello all! I have a problem with a double exponential equation. this are my data's structure(list(proc = c(1870.52067384719, 766.789388745793, 358.701545859122, 237.113777545511, 43.2726259059654, 148.985133316262, 92.6242882655781, 88.4521557193262, 56.6404686159112, 27.0374477259404, 34.3347291080268, 18.3226992991316, 15.2196612445747, 5.31600719692165, 16.7015717397302, 16.3923389973684, 24.2702542054496, 21.247247993673, 18.3070717608672, 2.8811892177331, 3.18018869564679, 8.74204132937479, 7.11596966047229 ), cls = c(0.25, 0.5, 0.75, 1, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10)), .Names = c(proc, cls), row.names = c(0.25, 0.5, 0.75, 1, 11, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10), class = data.frame) I want to compute a double exponential equation like this: proc=a*exp(b*class)+c*exp(d*class) or proc=a*exp(b*class)+c*class or a power, logarithmic equation. Is there a possibility to calculate R squared for each model? Thank you! -- --- Catalin-Constantin ROIBU Lecturer PhD, Forestry engineer Forestry Faculty of Suceava Str. Universitatii no. 13, Suceava, 720229, Romania office phone +4 0230 52 29 78, ext. 531 mobile phone +4 0745 53 18 01 +4 0766 71 76 58 FAX:+4 0230 52 16 64 silvic.usv.ro [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] non linear equation
Please do not forget to CC the list to increase your chances of getting help. --JIV On Wed, Apr 10, 2013 at 9:31 PM, catalin roibu catalinro...@gmail.comwrote: Hello! I try to compute the double exponential function this code: f-function(cls,a,b,c,d)a*exp(cls*b)+c*exp(cls*d) n2-nls(proc~f(cls,a,b,c,d),data=bline,start=list(a=600,b=-.1,c=4,d=-.1),trace=TRUE) proc~f(cls,a,b,c,d) but don't work! Thanks! On 10 April 2013 12:47, catalin roibu catalinro...@gmail.com wrote: Hello! I try to nls and I compute a simple exponential equation, but the problem in nls is to anticipate the regression coefficients. On 10 April 2013 12:19, Jorge I Velez jorgeivanve...@gmail.com wrote: Dear Catalin, You can look at ?nls. Alternatively, you could also consider a linear model as follows, where d is your data: # plot your data with(d, plot(cls, proc, las = 1)) # linear model fit - lm(proc ~ I(1/cls) + I((1/cls)^2), data = d) summary(fit) # plotting with(d, plot(cls, proc, las = 1)) grid - seq(min(d$cls), max(d$cls), length = 1000) points(grid, predict(fit, data.frame(cls = grid)), type = l, col = 2) HTH, Jorge.- On Wed, Apr 10, 2013 at 7:01 PM, catalin roibu catalinro...@gmail.comwrote: Hello all! I have a problem with a double exponential equation. this are my data's structure(list(proc = c(1870.52067384719, 766.789388745793, 358.701545859122, 237.113777545511, 43.2726259059654, 148.985133316262, 92.6242882655781, 88.4521557193262, 56.6404686159112, 27.0374477259404, 34.3347291080268, 18.3226992991316, 15.2196612445747, 5.31600719692165, 16.7015717397302, 16.3923389973684, 24.2702542054496, 21.247247993673, 18.3070717608672, 2.8811892177331, 3.18018869564679, 8.74204132937479, 7.11596966047229 ), cls = c(0.25, 0.5, 0.75, 1, 1, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10)), .Names = c(proc, cls), row.names = c(0.25, 0.5, 0.75, 1, 11, 1.5, 2, 2.5, 3, 3.5, 4, 4.5, 5, 5.5, 6, 6.5, 7, 7.5, 8, 8.5, 9, 9.5, 10), class = data.frame) I want to compute a double exponential equation like this: proc=a*exp(b*class)+c*exp(d*class) or proc=a*exp(b*class)+c*class or a power, logarithmic equation. Is there a possibility to calculate R squared for each model? Thank you! -- --- Catalin-Constantin ROIBU Lecturer PhD, Forestry engineer Forestry Faculty of Suceava Str. Universitatii no. 13, Suceava, 720229, Romania office phone +4 0230 52 29 78, ext. 531 mobile phone +4 0745 53 18 01 +4 0766 71 76 58 FAX:+4 0230 52 16 64 silvic.usv.ro [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- --- Catalin-Constantin ROIBU Lecturer PhD, Forestry engineer Forestry Faculty of Suceava Str. Universitatii no. 13, Suceava, 720229, Romania office phone +4 0230 52 29 78, ext. 531 mobile phone +4 0745 53 18 01 +4 0766 71 76 58 FAX:+4 0230 52 16 64 silvic.usv.ro -- --- Catalin-Constantin ROIBU Lecturer PhD, Forestry engineer Forestry Faculty of Suceava Str. Universitatii no. 13, Suceava, 720229, Romania office phone +4 0230 52 29 78, ext. 531 mobile phone +4 0745 53 18 01 +4 0766 71 76 58 FAX:+4 0230 52 16 64 silvic.usv.ro [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Generating a bivariate joint t distribution in R
Dear Miao, Check require(MASS) ?mvrnorm for some ideas. HTH, Jorge.- On Wed, Apr 3, 2013 at 4:57 PM, jpm miao wrote: Hi, I conduct a panel data estimation and obtain estimators for two of the coefficients beta1 and beta2. R tells me the mean and covariance of the distribution of (beta1, beta2). Now I would like to find the distribution of the quotient beta1/beta2, and one way to do it is to simulate via the joint distribution (beta1, beta2), where both beta1 and beta2 follow t distribution. How could we generate a joint t distrubuition in R? Thanks Miao [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] 95% Confidence Interval for a p-p plot
Pablo, Check the qqPlot function in car: require(car) qqPlot(x, dist = gamma, shape = 1.7918012, rate = 0.9458022) Best, Jorge.- On Tue, Apr 2, 2013 at 4:41 AM, pablo.castano wrote: Hi, I want to create upper and lower 95% confidence intervals for a p-p plot of an empirical distribution with a theoretical gamma distribution. This is my code: x-rgamma(100,shape=2, rate=1) # empirical data fitdistr(x,gamma) # fit a gamma distribution dist-pgamma(x,shape=1.9884256 ,rate=0.8765314 ) # fitted distribution, using the loglikelihood estimated parameters plot(ppoints(n=100), sort(dist)) # create p-p plot abline(0,1) # diagonal line to check fit Is there an implementation of the delta method in R that I could use for this in order to estimate the variance of the predicted probability ? Regards -- View this message in context: http://r.789695.n4.nabble.com/95-Confidence-Interval-for-a-p-p-plot-tp4662982.html Sent from the R help mailing list archive at Nabble.com. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Faster way of summing values up based on expand.grid
Hi Dimitri, If I understood correctly, the following will do: system.time(sum1 - apply(mycombos, 1, function(x) sum(values1[x]))) system.time(sum2 - apply(mycombos, 1, function(x) sum(values2[x]))) system.time(sum3 - apply(mycombos, 1, function(x) sum(values3[x]))) cbind(sum1, sum2, sum3) HTH, Jorge.- On Tue, Mar 26, 2013 at 8:12 AM, Dimitri Liakhovitski wrote: This is another method I can think of, but it's also slow: for(i in 1:nrow(mycombos)){ # i=1 indexes=rep(0,10) myitems-unlist(mycombos[i,1:4]) indexes[myitems]-1 mycombos$sum1[i]-sum(values1 * indexes) mycombos$sum2[i]-sum(values2 * indexes) mycombos$sum3[i]-sum(values3 * indexes) } On Mon, Mar 25, 2013 at 5:00 PM, Dimitri Liakhovitski dimitri.liakhovit...@gmail.com wrote: Hello! # I have 3 vectors of values: values1-rnorm(10) values2-rnorm(10) values3-rnorm(10) # In real life, all 3 vectors have a length of 25 # I create all possible combinations of 4 based on 10 elements: mycombos-expand.grid(1:10,1:10,1:10,1:10) dim(mycombos) # Removing rows that contain pairs of identical values in any 2 of these columns: mycombos-mycombos[!(mycombos$Var1 == mycombos$Var2),] mycombos-mycombos[!(mycombos$Var1 == mycombos$Var3),] mycombos-mycombos[!(mycombos$Var1 == mycombos$Var4),] mycombos-mycombos[!(mycombos$Var2 == mycombos$Var3),] mycombos-mycombos[!(mycombos$Var2 == mycombos$Var4),] mycombos-mycombos[!(mycombos$Var3 == mycombos$Var4),] dim(mycombos) # I want to write sums of elements from values1, values2, and values 3 whose numbers are contained in each column of mycombos. Here is how I am going it now - using a loop: mycombos$sum1-NA mycombos$sum2-NA mycombos$sum3-NA for(i in 1:nrow(mycombos)){ mycombos$sum1[i]-values1[[mycombos[i,Var1]]] + values1[[mycombos[i,Var2]]] + values1[[mycombos[i,Var3]]] + values1[[mycombos[i,Var4]]] mycombos$sum2[i]-values2[[mycombos[i,Var1]]] + values2[[mycombos[i,Var2]]] + values2[[mycombos[i,Var3]]] + values2[[mycombos[i,Var4]]] mycombos$sum3[i]-values3[[mycombos[i,Var1]]] + values3[[mycombos[i,Var2]]] + values3[[mycombos[i,Var3]]] + values3[[mycombos[i,Var4]]] } head(mycombos);tail(mycombos) # It's going to take me forever with this loop. Is there a faster way of doing the dame thing? Thanks a lot! -- Dimitri Liakhovitski -- Dimitri Liakhovitski [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] trouble with data frame
Sahana, The notation df[a,b)] is plain wrong. I think you meant (but I may be mistaken) df[a, b] and I am not still sure if that would work in your example. Have you instead considered subset()? E.g., subset(df, a = 10 b = 10) See ?subset for more details. Also, df is a very bad name for your data.frame. Check ?df to know why. HTH, Jorge.- On Fri, Mar 22, 2013 at 10:34 PM, Sahana Srinivasan wrote: Hi everyone, I am trying to use the values from every cell of the data frame in a further calculation. This is the code that I am using to catch every element of the data-frame. while (a=10) { while (b=10) { n-as.numeric(df[a,b)]; ...; } } The problem is that when I print out 'n' I get the following errors : NULL (if printed without as.numeric), and numeric(0) if printed with the as.numeric. Again, if I use the same command without the loop, it gives the correct answer. Would be grateful for your inputs and ideas on this matter. Thanks in advance, Sahana. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] problem subsetting data.frame in R version 2.15.2 for Windows
Or simply subset(dat, a 0) HTH, Jorge.- On Thu, Mar 21, 2013 at 6:58 PM, Michael Weylandt wrote: On Mar 21, 2013, at 7:39, Pierrick Bruneau pbrun...@gmail.com wrote: Hi Borja, You may issue: attach(data) No -- bad idea -- dangerous -- confusing statefulness, etc. (See explanations in the archives as to why) which results in adding your column names to the search path of R for name resolving. Pierrick Bruneau CRP Gabriel Lippmann On Wed, Mar 20, 2013 at 11:17 PM, Borja . borjalato...@outlook.com wrote: Good day. I create a data frame like this: data - data.frame(a=1:10,b=11:20,c=21:30) I can subset this data.frame by saying: data[data$a7,] and I get this result a b c8 8 18 28 9 9 19 29 10 10 20 30 I understand I should get the same result by saying data[a7,0] but I don't. Instead I get: Error in `[.data.frame`(data, a 7, 0) : object 'a' not Try instead with(dat, dat[a 0, ]) for a cleaner option. MW found Thank you very much in advance [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Help on indicator variables
Try ifelse(ABS ==1 | DEFF == 1, 1, 0) HTH, Jorge.- On Fri, Mar 22, 2013 at 12:02 AM, Tasnuva Tabassum t.tasn...@gmail.comwrote: I have two indicator variables ABS and DEFF. I want to create another indicator variable which will take value 1 if either ABS=1 or DEFF=1. Otherwise, it will take value 0. How can I make that? [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Counting confidence intervals
Hi Jim, Try either of the following (untested): sum( x[1, ] 12 x[2, ] 12) sum(apply(x, 2, function(x) x[1] 12 x[2] 12)) where x is your 2x1000 matrix. HTH, Jorge.- On Tue, Mar 19, 2013 at 12:03 AM, Jim Silverton wrote: Hi, I have a 2 x 1 matrix of confidence intervals. The first column is the lower and the next column is the upper. I want to cont how many times a number say 12 lies in the interval. Can anyone assist? -- Thanks, Jim. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Counting confidence intervals
Thats cumbersome, Arun. sum(mat1[,1] 12 mat1[,2] 12) [1] 17 will do the job and even faster: system.time(replicate(1, sum(mat1[,1] 12 mat1[,2] 12))) # user system elapsed # 0.067 0.001 0.078 HTH, Jorge.- On Tue, Mar 19, 2013 at 1:06 AM, arun wrote: Hi, Try this: set.seed(25) mat1- matrix(cbind(sample(1:15,20,replace=TRUE),sample(16:30,20,replace=TRUE)),ncol=2) nrow(mat1[sapply(seq_len(nrow(mat1)),function(i) any(seq(mat1[i,1],mat1[i,2])==12)),]) #[1] 17 set.seed(25) mat2- matrix(cbind(sample(1:15,1e5,replace=TRUE),sample(16:30,1e5,replace=TRUE)),ncol=2) system.time(res-nrow(mat2[sapply(seq_len(nrow(mat2)),function(i) any(seq(mat2[i,1],mat2[i,2])==12)),])) # user system elapsed # 1.552 0.000 1.549 res #[1] 80070 head(mat2[sapply(seq_len(nrow(mat2)),function(i) any(seq(mat2[i,1],mat2[i,2])==12)),]) # [,1] [,2] #[1,]7 29 #[2,] 11 30 #[3,]3 30 #[4,]2 26 #[5,] 10 22 #[6,]6 22 A.K. From: Jim Silverton To: r-help@r-project.org Sent: Monday, March 18, 2013 9:03 AM Subject: Re: [R] Counting confidence intervals Hi, I have a 2 x 1 matrix of confidence intervals. The first column is the lower and the next column is the upper. I want to cont how many times a number say 12 lies in the interval. Can anyone assist? -- Thanks, Jim. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Counting confidence intervals
If you don't use apply() it would be even faster: system.time(sum(mat2[,1] 12 mat2[,2] 12)) user system elapsed 0.004 0.000 0.003 Regards, Jorge.- On Tue, Mar 19, 2013 at 1:21 AM, arun wrote: Hi, Jorge's method will be faster. #system.time(res1-sum(apply(mat2,1,function(x) x[1]12 x[2]12))) #instead of 2, it should be 1 # user system elapsed # 0.440 0.000 0.445 system.time(res1-sum(apply(mat2,1,function(x) x[1]=12 x[2]12))) # # user system elapsed # 0.500 0.000 0.502 res1 #[1] 80070 A.K. From: Jim Silverton jim.silver...@gmail.com To: arun smartpink...@yahoo.com Sent: Monday, March 18, 2013 10:08 AM Subject: Re: [R] Counting confidence intervals thanks arun!! On Mon, Mar 18, 2013 at 10:06 AM, arun smartpink...@yahoo.com wrote: Hi, Try this: set.seed(25) mat1- matrix(cbind(sample(1:15,20,replace=TRUE),sample(16:30,20,replace=TRUE)),ncol=2) nrow(mat1[sapply(seq_len(nrow(mat1)),function(i) any(seq(mat1[i,1],mat1[i,2])==12)),]) #[1] 17 set.seed(25) mat2- matrix(cbind(sample(1:15,1e5,replace=TRUE),sample(16:30,1e5,replace=TRUE)),ncol=2) system.time(res-nrow(mat2[sapply(seq_len(nrow(mat2)),function(i) any(seq(mat2[i,1],mat2[i,2])==12)),])) # user system elapsed # 1.552 0.000 1.549 res #[1] 80070 head(mat2[sapply(seq_len(nrow(mat2)),function(i) any(seq(mat2[i,1],mat2[i,2])==12)),]) # [,1] [,2] #[1,]7 29 #[2,] 11 30 #[3,]3 30 #[4,]2 26 #[5,] 10 22 #[6,]6 22 A.K. From: Jim Silverton jim.silver...@gmail.com To: r-help@r-project.org Sent: Monday, March 18, 2013 9:03 AM Subject: Re: [R] Counting confidence intervals Hi, I have a 2 x 1 matrix of confidence intervals. The first column is the lower and the next column is the upper. I want to cont how many times a number say 12 lies in the interval. Can anyone assist? -- Thanks, Jim. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Thanks, Jim. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] string split at xth position
Dear Johannes, May not be the best way, but this looks like what you described: x - c(a1b1,a2b2,a1b2) x [1] a1b1 a2b2 a1b2 substr(x, 1, 2) [1] a1 a2 a1 substr(x, 3, 4) [1] b1 b2 b2 HTH, Jorge.- On Wed, Mar 13, 2013 at 7:37 PM, Johannes Radinger wrote: Hi, I have a vector of strings like: c(a1b1,a2b2,a1b2) which I want to spilt into two parts like: c(a1,a2,a2) and c(b1,b2,b2). So there is always a first part with a+number and a second part with b+number. Unfortunately there is no separator I could use to directly split the vectors.. Any idea how to handle such cases? /Johannes __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] merge datas
Dear Catalun, If I understood your description, please see ?%in% and try subset(x, names(x) %in% c(1834,1876,1901,1928,2006) ) where x is your data. HTH, Jorge.- On Wed, Mar 13, 2013 at 9:25 PM, catalin roibu wrote: Hello all! I have a problem with R. I try to merge data like this: structure(c(2.1785, 1.868, 2.1855, 2.5175, 2.025, 2.435, 1.809, 1.628, 1.327, 1.3485, 1.4335, 2.052, 2.2465, 2.151, 1.7945, 1.79, 1.6055, 1.616, 1.633, 1.665, 2.002, 2.152, 1.736, 1.7985, 1.9155, 1.7135, 1.548, 1.568, 1.713, 2.079, 1.875, 2.12, 2.072, 1.906, 1.4645, 1.3025, 1.407, 1.5445, 1.437, 1.463, 1.5235, 1.609, 1.738, 1.478, 1.573, 1.0465, 1.429, 1.632, 1.814, 1.933, 1.63, 1.482, 1.466, 1.4025, 1.6055, 1.279, 1.827, 1.201, 1.425, 1.678, 1.5535, 1.599, 1.826, 1.964, 1.68, 1.492, 1.509, 1.666, 1.5665, 1.666, 1.4885, 1.8205, 1.5965, 1.84, 1.551, 1.4835, 1.805, 1.7145, 1.902, 1.2085, 0.9095, 0.9325, 1.34, 1.6135, 1.5825, 1.757, 1.7105, 1.3115, 1.288, 1.567, 1.7795, 1.642, 1.4375, 1.4495, 1.4225, 1.4885, 1.251, 1.179, 1.188, 1.3605, 1.373, 1.2185, 1.405, 1.016, 0.979, 1.018, 1.0335, 1.39, 1.3005, 1.3955, 1.301, 1.6475, 1.1945, 1.3215, 1.0535, 1.1645, 1.0895, 1.041, 1.155, 1.322, 1.1615, 0.933, 1.1215, 1.022, 0.922, 0.8465, 1.103, 1.1375, 1.23, 1.289, 1.222, 1.4865, 1.4025, 1.4295, 1.156, 0.9085, 0.8755, 0.9135, 0.982, 1.145, 1.1295, 1.3475, 1.2415, 1.2505), .Names = c(1868, 1869, 1870, 1871, 1872, 1873, 1874, 1875, 1876, 1877, 1878, 1879, 1880, 1881, 1882, 1883, 1884, 1885, 1886, 1887, 1888, 1889, 1890, 1891, 1892, 1893, 1894, 1895, 1896, 1897, 1898, 1899, 1900, 1901, 1902, 1903, 1904, 1905, 1906, 1907, 1908, 1909, 1910, 1911, 1912, 1913, 1914, 1915, 1916, 1917, 1918, 1919, 1920, 1921, 1922, 1923, 1924, 1925, 1926, 1927, 1928, 1929, 1930, 1931, 1932, 1933, 1934, 1935, 1936, 1937, 1938, 1939, 1940, 1941, 1942, 1943, 1944, 1945, 1946, 1947, 1948, 1949, 1950, 1951, 1952, 1953, 1954, 1955, 1956, 1957, 1958, 1959, 1960, 1961, 1962, 1963, 1964, 1965, 1966, 1967, 1968, 1969, 1970, 1971, 1972, 1973, 1974, 1975, 1976, 1977, 1978, 1979, 1980, 1981, 1982, 1983, 1984, 1985, 1986, 1987, 1988, 1989, 1990, 1991, 1992, 1993, 1994, 1995, 1996, 1997, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010, 2011)) with a vector like this: extr-c(1834,1876,1901,1928,2006) The results must be like this: row.names MCG3 extr only for the extr values. is possible to do this with R? Thank you! -- --- Catalin-Constantin ROIBU Forestry engineer, PhD Forestry Faculty of Suceava Str. Universitatii no. 13, Suceava, 720229, Romania office phone +4 0230 52 29 78, ext. 531 mobile phone +4 0745 53 18 01 +4 0766 71 76 58 FAX:+4 0230 52 16 64 silvic.usv.ro [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Extract letters from a column
Dear SH, Hmmm... what about substr(tempdf$name, 4, 6)) ? HTH, Jorge.- On Thu, Mar 14, 2013 at 1:06 AM, SH empti...@gmail.com wrote: Dear list: I would like to extract three letters from first and second elements in one column and make a new column. For example below, tempdf = read.table(clipboard, header=T, sep='\t') tempdf name var1 var2abb 1 Tom Cruiser16 TomCru 2 Bread Pett25 BrePet 3 Arnold Schwiezer37 ArnSch (p1 = substr(tempdf$name, 1, 3)) [1] Tom Bre Arn I was able to extract three letters from first name, however, I don't know how to extract three letters from last name (i.e., 'Cru', 'Pet', and 'Sch'). Can anyone give me a suggestion? Many thanks in advance. Best, Steve __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Extract letters from a column
Try substr(tempdf$abb 4, 6) --JIV On Thu, Mar 14, 2013 at 1:15 AM, SH empti...@gmail.com wrote: Dear Jorge, I gave me this result (below) since it defines starting from the forth letter and ending 6th letter from the first element. substr(tempdf$name, 4, 6) [1] Cr ad old I would like to have letters from first and second elements if possible. Thanks for replying, Steve On Wed, Mar 13, 2013 at 10:10 AM, Jorge I Velez jorgeivanve...@gmail.com wrote: Dear SH, Hmmm... what about substr(tempdf$name, 4, 6)) ? HTH, Jorge.- On Thu, Mar 14, 2013 at 1:06 AM, SH empti...@gmail.com wrote: Dear list: I would like to extract three letters from first and second elements in one column and make a new column. For example below, tempdf = read.table(clipboard, header=T, sep='\t') tempdf name var1 var2abb 1 Tom Cruiser16 TomCru 2 Bread Pett25 BrePet 3 Arnold Schwiezer37 ArnSch (p1 = substr(tempdf$name, 1, 3)) [1] Tom Bre Arn I was able to extract three letters from first name, however, I don't know how to extract three letters from last name (i.e., 'Cru', 'Pet', and 'Sch'). Can anyone give me a suggestion? Many thanks in advance. Best, Steve __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] Extract letters from a column
Try x - c(Tom Cruiser, Bread Pett, Arnold Schwiezer) sapply(strsplit(x, ), function(r) paste0(substr(r[1], 1, 3), substr(r[2], 1, 3))) [1] TomCru BrePet ArnSch HTH, Jorge.- On Thu, Mar 14, 2013 at 1:21 AM, SH empti...@gmail.com wrote: What I want to do is to extrac three letters from first and last name and to combine them to make another column 'abb'. The column 'abb' is to be a my final product. I can make column 'abb' using 'paste' function once I have two parts from the first column 'name'. Thanks, Steve On Wed, Mar 13, 2013 at 10:17 AM, Jorge I Velez jorgeivanve...@gmail.com wrote: Try substr(tempdf$abb 4, 6) --JIV On Thu, Mar 14, 2013 at 1:15 AM, SH empti...@gmail.com wrote: Dear Jorge, I gave me this result (below) since it defines starting from the forth letter and ending 6th letter from the first element. substr(tempdf$name, 4, 6) [1] Cr ad old I would like to have letters from first and second elements if possible. Thanks for replying, Steve On Wed, Mar 13, 2013 at 10:10 AM, Jorge I Velez jorgeivanve...@gmail.com wrote: Dear SH, Hmmm... what about substr(tempdf$name, 4, 6)) ? HTH, Jorge.- On Thu, Mar 14, 2013 at 1:06 AM, SH empti...@gmail.com wrote: Dear list: I would like to extract three letters from first and second elements in one column and make a new column. For example below, tempdf = read.table(clipboard, header=T, sep='\t') tempdf name var1 var2abb 1 Tom Cruiser16 TomCru 2 Bread Pett25 BrePet 3 Arnold Schwiezer37 ArnSch (p1 = substr(tempdf$name, 1, 3)) [1] Tom Bre Arn I was able to extract three letters from first name, however, I don't know how to extract three letters from last name (i.e., 'Cru', 'Pet', and 'Sch'). Can anyone give me a suggestion? Many thanks in advance. Best, Steve __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] take two columns from a set of lists
Is the following that you are looking for? unlist(lapply(x.list, [, 2)) HTH, Jorge.- On Mon, Mar 11, 2013 at 9:52 PM, ishi soichi wrote: say I have a matrix and lists like x - matrix(c(12.1, 3.44, 0.1, 3, 12, 33.1, 1.1, 23), nrow=2) x.list - lapply(seq_len(nrow(x)), function(i) x[i,]) if I want a column of the matrix x, I write x[, 2] for example. But how can I do something similar for a set of lists, x.list, above? x.list [[1]] [1] 12.1 0.1 12.0 1.1 [[2]] [1] 3.44 3.00 33.10 23.00 unlist(x.list)[,2] does not work. Anyone? ishida [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] transpose lists
One option would be x - list(c(12.1, 0.1, 12, 1.1), c(3.44, 3, 33.1, 23)) do.call(c, apply(do.call(rbind, x), 2, list)) HTH, Jorge.- On Fri, Mar 8, 2013 at 9:06 PM, ishi soichi soichi...@gmail.com wrote: Thanks. The result should be a list of lists like... x [[1]] [1] 12.10 3.44 [[2]] [1] 0.1 3.0 [[3]] [1] 12.0 33.1 [[4]] [1] 1.1 23.0 lapply(x, t) doesn't do the job, I think. ishida 2013/3/8 PIKAL Petr petr.pi...@precheza.cz Hi -Original Message- From: r-help-boun...@r-project.org [mailto:r-help-bounces@r- project.org] On Behalf Of ishi soichi Sent: Friday, March 08, 2013 10:50 AM To: r-help Subject: [R] transpose lists Can you think of a function that transposes a list like What shall be the result of transposed list? Something like lapply(x, t) Regards Petr x [[1]] [1] 12.1 0.1 12.0 1.1 [[2]] [1] 3.44 3.00 33.10 23.00 ? ishida [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting- guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to export data with defined decimal places
Dear Marin, May be not the cleanest way to do it, but the following seems to work: write.table(as.character(round(pi, 10)), pi.txt, row.names = FALSE, col.names = FALSE, quote = FALSE) Best, Jorge.- On Fri, Mar 8, 2013 at 11:24 AM, Marino David davidmarino...@gmail.comwrote: Hi Bert, I read both options and write.table help, but I still can't make it to save the data into txt file with fixed precision. To let you know more clearly what I want, I still you use the previous simple example to illustrate. I want to save pi into pi.txt file with 10 decimal places, that is 3.1415926536. How to do it? Thanks Marin 2013/3/8 Marino David davidmarino...@gmail.com Hi Bert, I want to save the data into .txt file for another software process. Thanks for suggestion. 2013/3/8 Bert Gunter gunter.ber...@gene.com ?write.table which says, under details: In almost all cases the conversion of numeric quantities is governed by the option scipen (see options), but with the internal equivalent of digits=15. For finer control, use format to make a character matrix/data frame, and call write.table on that. Not sure if this is what you want, as export is rather vague. -- Bert On Thu, Mar 7, 2013 at 12:52 PM, Marino David davidmarino...@gmail.com wrote: Hi all mailing listers, I want to export data with specified precision into .txt file. How can I make it? See below sprintf(%.10f,pi) [1] 3.1415926536 when carry out write.matrix(pi,pi.txt), 3.141592653589793115998 in pi.txt file not with 10 decimal places like using sprintf(%.10f,pi) Thanks Marino [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html http://www.r-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. -- Bert Gunter Genentech Nonclinical Biostatistics Internal Contact Info: Phone: 467-7374 Website: http://pharmadevelopment.roche.com/index/pdb/pdb-functional-groups/pdb-biostatistics/pdb-ncb-home.htm [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] reduce the size of list
If I understood correctly, lapply(x, [, 1:3) will do what you want. HTH, Jorge.- On Fri, Mar 8, 2013 at 5:05 PM, ishi soichi wrote: hi. I have a list like x - list(1:10,11:20,21:30) It's a sort of a 3 x 10 matrix in list form. I would like to reduce the dimension of this list. it would be something like list(1:3, 11:13, 21,23) I tried x[,1:3] does not work of course. Neither lapply(x, [1:3]) works... Any suggestions? ishida [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] How to reference to the `stats` package in academical paper
Dear Julien, Check citation('stats') HTH, Jorge.- On Wed, Mar 6, 2013 at 12:05 AM, Julien Mvdb julien.m...@gmail.com wrote: The question is in the title. Then, I would like to know how I should refer to the documentation regarding the use of each functions. Thanks, Julien Mehl Vettori [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.
Re: [R] recode data according to quantile breaks
Hi Alain, The following should get you started: apply(df[,-1], 2, function(x) cut(x, breaks = quantile(x), include.lowest = TRUE, labels = 1:4)) Check ?cut and ?apply for more information. HTH, Jorge.- On Tue, Feb 19, 2013 at 9:01 PM, D. Alain wrote: Dear R-List, I would like to recode my data according to quantile breaks, i.e. all data within the range of 0%-25% should get a 1, 25%-50% a 2 etc. Is there a nice way to do this with all columns in a dataframe. e.g. df- f-data.frame(id=c(x01,x02,x03,x04,x05,x06),a=c(1,2,3,4,5,6),b=c(2,4,6,8,10,12),c=c(1,3,9,12,15,18)) df ida b c 1 x01 1 2 1 2 x02 2 4 3 3 x03 3 6 9 4 x04 4 8 12 5 x05 5 10 15 6 x06 6 12 18 #I can do it in very complicated way apply(df[-1],2,quantile) abc 0% 1.0 2.0 1.0 25% 2.2 4.5 4.5 50% 3.5 7.0 10.5 75% 4.8 9.5 14.2 100% 6.0 12.0 18.0 #then df$a[df$a=2.2]-1 ... #result should be df.breaks idabc x011 11 x021 11 x032 22 x043 33 x054 44 x064 44 But there must be a way to do it more elegantly, something like df.breaks- apply(df[-1],2,recode.by.quantile) Can anyone help me with this? Best wishes Alain [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code. [[alternative HTML version deleted]] __ R-help@r-project.org mailing list https://stat.ethz.ch/mailman/listinfo/r-help PLEASE do read the posting guide http://www.R-project.org/posting-guide.html and provide commented, minimal, self-contained, reproducible code.