I am very green to R so please bear with my wording. I have a df from a csv that has 106 obs of 11 variables. I only care about 2 of those variables so I made a new df called "df."
bc=read.csv("---.csv")
df=cbind.data.frame('A'=bc$A,'B'=bc$B)
#Example of the new df:
A B
mass 0.1
mass 0.2
height 0.5
height 0.3
color 0.9
color 0.1
Then I made (4) vectors, each based on how many rows could satisfy (2) simultaneous conditions: greater than OET or less than OET AND type is "mass" or type is not "mass."
TP= df[df$B>=i & df$A=="mass",]
TN= df[df$B<=i & df$A!="mass",]
FP= df[df$B<=i & df$A!="mass",]
FN= df[df$B<=i & df$A=="mass",]
I think I want to use a for loop so I could have a vector for every B condition, every i. If I set "i" to a value, the vectors will give me all rows that fit and then nrow("vector") to see how many rows that is- but I cannot type all 106 df$B values into i. I did print to see if my i would work and it showed that I could get every row from df$B. So then I tried with half of the TP vector with df$A. That worked. Now I tried the df$B part alone. But this gave me all 106 obs which I know is wrong becuse the non-looped TP gave me 21 obs. The end goal of the code is to give me a number of TP and and TN for every df$B that meets my (2) conditions so that I can plug them into another function to ggplot. [like Y=TP/TP-TN]
N=c(df$B)
for(i in N){
print(paste(i))
}
# worked
for(i in N){
TPA=df[df$A=="mass",]
TP=nrow(TPA)
}
# worked
for(i in N){
TPB=df[df$B>=i,]
TP=nrow(TPB)
}
#ran but did not do what I wanted
I guess my question is how do I run all rows of df$B against each df$B, all 106 of them, and store them?
When i = df$B[1], how many rows of df$B are >i
When i= df$B[2], how many rows of df$B are >i
From a formula like this, I would like an output like below:
results=data.frame(matrix(nrow=,ncol=4))
colnames(results)=c("A","B","TP","TN")
B=rep(c("mass","not mass"),each=106)
N=c(df$B)
for(i in N){
TPC=df[df$A=='mass' & df$B>=i,]
TP=nrow(TPC)
TNC=df[df$A!='mass' & df$B<=i,]
TN=nrow(TNC)
}
results=cbind.data.frame(B,A,results)
B A TP TN
mass df$B[1] 21 0
mass df$B[2] 18 12
...
notmass df$B[1] 1 11
notmass df$B[2] 3 10
...
If you read this far, thank you! Any direction or answer would be most appreciated!
FN= df[df$B<=i & df$A=="mass",]
line. Did you meandf$B >= i
perhaps? - Jon Spring