1

I have a little problem to do a movement in R. This is what my data looks like:

df1 <- read.csv(text="Time, Depth, Conductivity, Temperature, Pressure,Turbidity 
26/04/2018 15:12:53, 1.01087, 0.0388654, 8.23849, 11.1496,  46.5928
26/04/2018 15:12:53, 1.10582, 0.0388088, 8.23773, 11.2452,  47.8815
26/04/2018 15:12:53, 1.18670, 0.0394323, 8.23928, 11.3265,  47.2870 
26/04/2018 15:12:54, 1.26171, 0.0393001, 8.24116, 11.4020,  47.9308
26/04/2018 15:12:54, 1.47019, 0.0382799, 8.24026, 11.6118,  45.1384
26/04/2018 15:12:54, 1.60172, 0.0390357, 8.24002, 11.7441,  45.5681
26/04/2018 15:12:54, 1.73643, 0.0387522, 8.24033, 11.8797,  45.8713
26/04/2018 15:12:54, 1.87313, 0.0390168, 8.23834, 12.0172,  45.0766")

# I did this for my analysis and get a mean of every meter by depth (works pretty well):

library(stringr)
df1$time <- sapply(strsplit(as.character(as.factor(df1$Depth)), " "), "[", 1)
meter <- str_replace(df1$time,"\\.",paste0(":"))
meter <-unlist(lapply(strsplit(meter, "\\:"), function(x)x[1]))
df1$time <- NULL
df1$meter <- meter

l <- split(df1, df1$meter)
l.result <- lapply(l, function(x){mean(x$Temperature)})
l.result <- as.data.frame(unlist(l.result))
l.result$meter <- rownames(l.result)
names(l.result) <- c("mean","meter")
df1$meter <- NULL
l.result$meter <- as.numeric(l.result$meter)

l.result <- l.result[order(l.result$meter),]

# However, now I need eval the others variables of my data frame (Conductivity, Pressure, etc) and I was trying to do a loop and replace "x$Temperature" for "x$Conductivity", and then for x$Pressure (and the others variables). So I did this:

names <- c("x$Conductivity", "x$Temperature", "x$Pressure",
           "x$Turbidity", "x$Sea_Pressure", "x$Salinity")

for(i in length(names)){

tmp <- names[i] # (temporal variable)

df1$time <- sapply(strsplit(as.character(as.factor(df1$Depth)), " "), "[", 1)
meter <- str_replace(df1$time,"\\.",paste0(":"))
meter <-unlist(lapply(strsplit(meter, "\\:"), function(x)x[1]))
df1$time <- NULL
df1$meter <- meter

l <- split(df1, df1$meter)
l.result<- lapply(l, function(x){mean(assign(tmp,names[i])}) #*Here's the problem
l.result <- as.data.frame(unlist(l.result))
l.result$meter <- rownames(l.result)
names(l.result) <- c("mean","meter")
df1$meter <- NULL
l.result$meter <- as.numeric(l.result$meter)

l.result <- l.result[order(l.result$meter),]

}

So, just I need the correct way to use assign, or maybe another option.

MrFlick
  • 195,160
  • 17
  • 277
  • 295
  • 1
    Please share your data in a [reproducible format](https://stackoverflow.com/questions/5963269/how-to-make-a-great-r-reproducible-example). Many of the `library()` calls you included don't seem relevant. Also, clearly state what the desired outcome is. It's unclear what this code is trying to do or what the outcome would be. I think you can do this all with a simple `dplyr` chain, for example: `df1 %>% group_by(meter=floor(Depth)) %>% summarize_at(vars(-Time, -Depth), mean)` – MrFlick Jul 04 '18 at 23:09
  • `lapply(c("Conductivity","Temperature"), function(nm) { ... df1[[nm]] ...})` – r2evans Jul 05 '18 at 00:35
  • MrFlick has given the precise solution to your problem. Check whether it doesn't solve your issues – Onyambu Jul 05 '18 at 04:51
  • It works perfectly Mr Flick! I'm going to take your suggestion and I'll be more careful with the format the next time. Well, thank you so much for your time guys and of course for the solution Mr Flick! – Nicus D. García Jul 06 '18 at 23:06

0 Answers0