I have a n by m data.frame
where column 1 has the information of interest. I want to create sub data.frame
s based upon what the value in a row of colum
Instead of :
s <- data.frame(df1$P = S)
try:
s <- data.frame(df1[df1$P == S,])
or
s <- data.frame(df1[df1$P == 'S',])
if you want to control number of rows try:
s <- data.frame(df1[df1$P == 'S',1:5])
Breaking your data into a list
of data.frames
using split
may also work here, and prevent cluttering your workspace. E.g:
df1 <- data.frame(P=c("S","S","A","I"),data=rep(1,4))
df1
# P data
#1 S 1
#2 S 1
#3 A 1
#4 I 1
result <- split(df1,df1$P)
#$A
# P data
#3 A 1
#
#$I
# P data
#4 I 1
#
#$S
# P data
#1 S 1
#2 S 1
You can then access the parts of the list like:
result$S
or
result[["S"]]
Voila:
# P data
#1 S 1
#2 S 1