[R-es] aplicar codigo

2020-09-09 Thread Samura .
Hola,
me gustar�a hacer algo como en el siguiente ejemplo

A un df a�adirle una columna que es la transformaci�n de otra,
en plan a todo lo que sea  x1, x2, x3 lo llamo prueba 1
todo lo que sea x4,x5,x6 lo llamo prueba 2
el resto de x las dejo como est�n.

Ser�a algo as�

col1 <- c('x1', 'x2', 'x11', 'x1','x33', 'x1','x4', 'x5', 'x35', 'x1','x2', 
'x4')
df1<-data.frame(col1)
attach(df1)

df1$transformacion<-ifelse(col1 == "x1"|col1 == "x2"| col1 == "x3", "prueba1",
   ifelse(col1 == "x4"|col1 == "x5"| col1 == "x6", 
"prueba2", col1))

detach(df1)
df1

pero ahora en vez de un df tengo varios

col2 <- c('x12', 'x4', 'x6', 'x771','x4', 'x2')
col3 <- c('x7', 'x2', 'x4', 'x5','x111', 'x1','x4', 'x5', 'x35','x2','x2', 
'x4','x6', 'x5')
df2<-data.frame(col2)
df3<-data.frame(col3)

�c�mo puedo aplicar el c�digo al resto de los df sin tener que repetirlo?



[[alternative HTML version deleted]]

___
R-help-es mailing list
R-help-es@r-project.org
https://stat.ethz.ch/mailman/listinfo/r-help-es


[R] facet_wrap(nrow) ignored

2020-09-09 Thread Ivan Calandra
Dear useRs,

I have an issue with the argument nrow of ggplot2::facet_wrap().

Let's consider some sample data:
mydf <- data.frame(grp = rep(letters[1:6], each = 15), cat = rep(1:3,
30), var = rnorm(90))

And let's try to plot with 5 rows:
library(ggplot2)
ggplot(data = mydf, aes(x = cat, y = var)) + geom_point() +
facet_wrap(~grp, nrow = 5)
It plots 2 rows and 3 columns rather than 5 rows and 2 columns as wanted.

These plots are as expected:
ggplot(data = mydf, aes(x = cat, y = var)) + geom_point() +
facet_wrap(~grp, nrow = 2)
ggplot(data = mydf, aes(x = cat, y = var)) + geom_point() +
facet_wrap(~grp, nrow = 6)

My guess is that 5 rows is not ideal for 6 facets (5 facets in 1st
column and only 1 facet for 2nd column) so it overrides the value of
nrow. In the case of 2 or 6 rows, the facets are well distributed in the
layout.

The reason why I need 5 rows with 6 facets is that this facet plot is
part of a patchwork and I would like to have the same number of rows for
all facet plots of the patchwork (so that they all align well).

Is there a way to force the number of rows in the facet_wrap()?

Thank you in advance.
Best,
Ivan

-- 


--
Dr. Ivan Calandra
TraCEr, laboratory for Traceology and Controlled Experiments
MONREPOS Archaeological Research Centre and
Museum for Human Behavioural Evolution
Schloss Monrepos
56567 Neuwied, Germany
+49 (0) 2631 9772-243
https://www.researchgate.net/profile/Ivan_Calandra

__
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.


[R] calculating a linear regression for each grid cell. firstly

2020-09-09 Thread ahmet varlı
   Hi all,


for example, � have 4 raster data and � try to convert these 
rasters to 1 array and calculate a linear regression for each grid cell how can 
� do this?



max_consecutive_days_1<- raster(ncol=94, nrow=192, xmn=-180, 
xmx=180, ymn=-90, ymx=90)
max_consecutive_days_2<- raster(ncol=94, nrow=192, xmn=-180, 
xmx=180, ymn=-90, ymx=90)
max_consecutive_days_3<- raster(ncol=94, nrow=192, xmn=-180, 
xmx=180, ymn=-90, ymx=90)
max_consecutive_days_4<- raster(ncol=94, nrow=192, xmn=-180, 
xmx=180, ymn=-90, ymx=90)

values(max_consecutive_days_1)  <- 
1:ncell(max_consecutive_days_1)
values(max_consecutive_days_2) <- 
1:ncell(max_consecutive_days_2)
values(max_consecutive_days_3) <- 
1:ncell(max_consecutive_days_3)
values(max_consecutive_days_4) <- 
1:ncell(max_consecutive_days_4)
set.seed(0)
values(max_consecutive_days_1)  <- 
runif(ncell(max_consecutive_days_1))
values(max_consecutive_days_2) <- 
runif(ncell(max_consecutive_days_2))
values(max_consecutive_days_3) <- 
runif(ncell(max_consecutive_days_3))
values(max_consecutive_days_4) <- 
runif(ncell(max_consecutive_days_4))

 I tried to convert raster to an array and calculate a linear 
regression for each cell with these codes. there is something wrong in codes 
but I didn't find what it's wrong

library(raster)
r<-raster("C:/max_consecutive_days_1.tif")
a<-array(NA,dim=c(dim(r)[1:2],4))
i <- 1
dir.in <- "C:/max_consecutive_days/"
for (year in 1:4) {
  fi<-paste0(dir.in,"max_consecutive_days_",year,".tif")
  r<-raster(fi)
  a[,,i]<-getValues(r,format="matrix")
  i<-i+1
}


 lmfunction <- function(x){

  if (is.na(x[1])){ NA }

  else

y<-c(1:length(x))
  m1<- summary(lm(x~y))
  coef <- m1$coef[2]

  coef
 }

storem <- array(NA,dim=dim(a)[1:2])
for(i in 1:94){
  for(j in 1:192){
tst<-mean(a[i,j,],n.rm=T)
if (is.na(tst)==F){
  storem[i,j]<-lmfunction(a[i,j,])
}
  }
}


Best wishes,

Windows 10 i�in Posta ile 
g�nderildi


[[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.