-3

I am stuck with the beginning of my analysis. Perhaps the question could be stupid, but I would like to request your help for some tips. I have a dataframe with several variables; and each variable has 10 observations. My doubt is how can I estimate for each variable the max of the first 5 observations, and the max of the following 5 observations.

This is an example of my code:

    for (i in 1:length(ncols)){
 max.value <- max(var1)
}

Thank you very much in advance

  • Can you provide a minimal reproducible example? – markus Jan 17 '19 at 12:01
  • Here goes and example with the dataset "iris" from vegan's package. In this case, I would like to estimate the max value of each variable for group of 50 observations. Notice that in this dataset, each group of 50 variables correspond with a one different level of the variable species. `library(vegan) data("iris") data.test <- iris[,1:4] for (i in 1:ncol(data.test)){ print (i) maxval <- max(i) }` thank you! – Alejandro López Núñez Jan 17 '19 at 12:28
  • Do you need `aggregate(cbind(Sepal.Length, Sepal.Width, Petal.Length, Petal.Width) ~ Species, iris, max)` ? – markus Jan 17 '19 at 12:35
  • I think you're real question is how to find a maximum per group? – erc Jan 17 '19 at 13:15
  • Thank you for your comments. The function aggregate works nicely :) Yes, my real question is how to find a max per group, but, without the group per se... I mean, I have not a variable group in my dataset, but I know a priori how many observations are included in each group. – Alejandro López Núñez Jan 17 '19 at 15:45

1 Answers1

0
# Load data
data(mtcars)

# Add running ID to check results if you want to
mtcars$ID <- seq.int(nrow(mtcars))

# Make index to separate data 50/50
ind <- seq_len(0.5*nrow(mtcars))

# Make two DFs "first..." and "second..."
firstpart <- mtcars[ind, ]
secondpart <- mtcars[-ind, ]

# Look at the results
View(firstpart)
View(secondpart)

# do your stuff to data
Peter
  • 2,120
  • 2
  • 19
  • 33