How can I loop through a determinated range of rows? [duplicate] - r

This question already has answers here:
Select the row with the maximum value in each group
(19 answers)
Closed 4 years ago.
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

# 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

Related

R Sample Programming: Show the absolute counts of female and male patients and status at end of observation in the sample [duplicate]

This question already has answers here:
Counting the frequency of an element in a data frame [duplicate]
(2 answers)
Closed 1 year ago.
Use the Aids2 dataset(see below). Use a sample size of 50 for each of the following.
Show the sample drawn using simple random sampling with replacement.
Show the frequencies (the absolute counts) of female and male patients (sex) and status at end of observation (status) in the sample.
packages prob and sampling may be used
My codes here:
aids <- read.csv(file="D:/Aids2.csv", header=T)
library(sampling)
nrow(aids)
s <- srswor(50, 2843)
rows <- (1:nrow(aids))[s!=0]
rows <- rep(rows, s[s!=0])
female <-rows[aids$sex=="F"]
male <- rows[aids$sex=="M"]
table(female)
table(male)
dead<-rows[aids$status=="D"]
alive<-rows[aids$status=="A"]
table(dead)
table(alive)
So its like I know everything runs fine, but Im not sure how to achieve exactly what the question asking. Can anyone help me to fix my script
links of the file: https://drive.google.com/file/d/1vVKYQ_oDu6Fv00P-vgypifxfMgnyV7qw/view?usp=sharing
I don't have access to the data but something like this should work.
aids <- read.csv(file="D:/Aids2.csv", header=T)
#Select 50 random rows with replacement
sample_data <- sample_data[sample(nrow(sample_data), 50, replace = TRUE), ]
#Count sex in sample_data
table(sample_data$sex)
#Count status
table(sample_data$status)

Find total frequency of number In Column [duplicate]

This question already has answers here:
Counting the number of elements with the values of x in a vector
(20 answers)
Closed 5 years ago.
I am new on R. I want to ask, How to find frequency of each Number in Column, there are multiple numbers in column. i want to frequency of each number. I want just simple code. You can imagine that data set name is Oct-TT. Thanks
Here is the answer:
df <- as.data.frame(sample(10:20, 20,replace=T))
colnames(df) <- "Numbers"
View(df)
as.data.frame(table(df$Numbers))

Optimizing too long loop in R [duplicate]

This question already has answers here:
How to reshape data from long to wide format
(14 answers)
Closed 6 years ago.
I'm quite new to R and really really need your help with my double for-loop which takes too much time to complete.
data a data table with 659322 rows and 3 columns (ID, Game, Amount)
Each ID is repeated several times (i.e. several Game for each ID), but unevenly distributed across the rows. We may have 2 Games for ID1 (so ID1 appears in 2 rows), 5 Games for ID2, 4 Games for ID3, etc.
I want to create a matrix datmat from data with:
- Nb of rows = nb of unique values of ID (nb_row=46028)
- Nb of columns = nb of unique values of Game (nb_col=30)
and fill in datmat with the corresponding Amount values
Here's what I tried
ID <- unique(data$ID)
Game <- unique(data$Game)
nb_row <- length(ID)
nb_col <- length(Game)
datmat <- matrix(c(0),nb_row,nb_col,dimnames=list(NULL,Game))
for(i in 1:nb_row){
for(j in 1:nb_col){
datmat[i,j] <- data$Amount[data$ID==ID[i] & data$Game==Game[j]]
}
}
dt <- data.table(ID,datmat)
Any suggestion could be greatly appreciated. Thank you all!
You might want to use the reshape function :
newdata<-reshape(data,timevar="Game",idvar="ID",direction="wide")

Deleting Rows in R [duplicate]

This question already has answers here:
Subset dataframe by multiple logical conditions of rows to remove
(8 answers)
Closed 7 years ago.
I'm trying to delete all rows in a dataframe when the average of a vector > an individual number in the vector. For some reason it seems to pick and choose which ones it deletes. All help is appreciated thank you, here is my code.
k<-c(HW2$AGE)
j<-mean(k)
for (i in HW2$AGE)
if (j>i){
HW2 <- HW2[-i, ]
}
Don't need to vectorise. Instead I would use the below
Sample data
x <- data.frame("A"= runif(10), "B" = runif(10))
Calculate mean
xMean <- mean(x[,"A"])
Exclude rows
y <- x[x$A < xMean,]
This is probably the most obvious way of excluding unwanted rows

how to find the complement of a random selection of rows of a dataframe? [duplicate]

This question already has answers here:
How to split a data frame?
(8 answers)
Closed 8 years ago.
I have a data set called data, which I am splitting into 2 new data sets, which I will call test and train.
I want the splitting to be random, without replacement.
Using the code below, I get train to be a new data frame with 35 elements:
rows_in_test <- 35 # number of rows to randomly select
rows_in_train <- nrow(data) - rows_in_test
train <- data[sample(nrow(data), rows_in_test), ]
Is there a nice way in R to assign the complement of train to a new data set called test? I am thinking there must be a function for this?
myData<-data.frame(a=c(1:20), b=c(101:120))
set.seed(123)#to be able to replicate random sampling later
trainRows<-runif(nrow(myData))>0.25 #randomly put aside 25% of the data
train<-myData[trainRows,]#has 13 rows
test<-myData[!trainRows,]#has 7 rows
#following method to select a fixed no. of samples - in this case selecting 5 rows
testRows2<-sort(sample(c(1:nrow(myData)), 5, replace=F))
train2<-myData[-testRows2, ]
test2<-myData[testRows2, ]

Resources