[R] select if + other questions
John Kane
jrkrideau at yahoo.ca
Thu Apr 26 23:13:32 CEST 2007
--- Natalie O'Toole <notoole at mtroyal.ca> wrote:
> Hi,
>
> i am trying to read a .txt file, do a couple of
> select if statements on my
> data, and then finally use the ?table function to
> get frequency counts on
> the data. Specifically, i am looking at answering
> the following question:
>
> What is the frequency of Grade 7 students in the
> province of Alberta who
> are smokers?
>
> I am having some problems:
>
> 1)i cannot get the column names to show up when
> print to screen
Are you sure they are there? Try mydata[1,] and see
if you get the names.
If not just assign the names by using
names (mydata) <- myVariableNames
>
> 2)I cannot seem to skip variables properly when i
> choose certain other variables
I don't quite understand what you mean here.
>
> 3)i cannot get the combination of Select If
> statements to work to produce
> a different table with my new criteria
Try subset rather than which.
subset(mydata, PROV=="AB" & GRADE == 7 & Y_Q10A != 9)
which() is a logical operator and as far as I am aware
only will take a TRUE FALSE reponse
which(mydata$PROV=="AB") # should work
> Here are the variables
>
> PUMFID position1 length 5
> PROV position 6 length 2
> GRADE position 9 length 2
> Y_Q10A position 33 length 1
>
>
> Y_Q10A has the following 1=yes
> 2=no
> 9=skip
>
> all the others have no skipped or missing values
>
> Here is my code:
>
> myfile<-("c:/test2.txt")
> myVariableNames<-c("PUMFID","PROV","GRADE","Y_Q10A")
> myVariableWidths<-c(5,2,2,1)
>
>
> mydata<-read.fwf(
> file=myfile,
> width=myVariableWidths,
> col.names=myVariableNames,
> row.names="PUMFID",
> fill=TRUE,
> strip.white=TRUE)
>
>
> print(mydata)
>
> print( mydata [which(PROV=="AB" & GRADE==7 &
> Y_Q10A<9), ] )
>
>
>
> Any help would be greatly appreciated!!
>
> Thank-you,
>
> Nat
>
>
More information about the R-help
mailing list