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 2)I cannot seem to skip variables properly when i choose certain other variables 3)i cannot get the combination of Select If statements to work to produce a different table with my new criteria 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 ------------------------------------------------------------------------------------------------------------------------ This communication is intended for the use of the recipient to which it is addressed, and may contain confidential, personal, and or privileged information. Please contact the sender immediately if you are not the intended recipient of this communication, and do not copy, distribute, or take action relying on it. Any communication received in error, or subsequent reply, should be deleted or destroyed. [[alternative HTML version deleted]]
--- 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 screenAre 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 variablesI 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 criteriaTry 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 > >
Hi, Thank-you for the response!! That worked great!! Is there any way to apply a weight variable to your file similar to what you can do in SPSS? So that all of your other variables will be weighted by the weight variable? Thanks, Nat __________________ 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 2)I cannot seem to skip variables properly when i choose certain other variables 3)i cannot get the combination of Select If statements to work to produce a different table with my new criteria 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 ------------------------------------------------------------------------------------------------------------------------ This communication is intended for the use of the recipient to which it is addressed, and may contain confidential, personal, and or privileged information. Please contact the sender immediately if you are not the intended recipient of this communication, and do not copy, distribute, or take action relying on it. Any communication received in error, or subsequent reply, should be deleted or destroyed. ------------------------------------------------------------------------------------------------------------------------ This communication is intended for the use of the recipient to which it is addressed, and may contain confidential, personal, and or privileged information. Please contact the sender immediately if you are not the intended recipient of this communication, and do not copy, distribute, or take action relying on it. Any communication received in error, or subsequent reply, should be deleted or destroyed. [[alternative HTML version deleted]]
Hi, Does anyone know how to skip variables (or columns) in R. Say, for example i had PUMFID position1 and Y_Q10A position 33 and i do not want to include all the variables in between. Is there a way to do this in R when you are extracting variables from a large .txt file with many, many variables? Thanks, Nat __________________ Yes but I believe it will vary depending on what package you're using. I don't deal with weigthed data so I'm not a good source Have a look at help for something like lm in the stats package (part of the base installation) for an example. ?lm weight is the fourth argument down. However for more information try http://finzi.psych.upenn.edu/search.html and type in weight. As Brian Ripley says in a reply to a question about weights: "Almost all methods I know of do: logistic regression, neural nets, classification trees, PPR .... " --- Natalie O'Toole <notoole@mtroyal.ca> wrote:> Hi, > > Thank-you for the response!! That worked great!! Is > there any way to apply > a weight variable to your file similar to what you > can do in SPSS? So that > all of your other variables will be weighted by the > weight variable? > > Thanks, > > Nat > > __________________ > > > 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 > > 2)I cannot seem to skip variables properly when i > choose certain other > variables > > 3)i cannot get the combination of Select If > statements to work to produce > a different table with my new criteria > > 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 > >------------------------------------------------------------------------------------------------------------------------> > > This communication is intended for the use of the > recipient to which it is > addressed, and may > contain confidential, personal, and or privileged > information. Please > contact the sender > immediately if you are not the intended recipient of > this communication, > and do not copy, > distribute, or take action relying on it. Any > communication received in > error, or subsequent > reply, should be deleted or destroyed. >------------------------------------------------------------------------------------------------------------------------> > > This communication is intended for the use of the > recipient to which it is > addressed, and may > contain confidential, personal, and or privileged > information. Please > contact the sender > immediately if you are not the intended recipient of > this communication, > and do not copy, > distribute, or take action relying on it. Any > communication received in > error, or subsequent > reply, should be deleted or destroyed. > [[alternative HTML version deleted]] > > ______________________________________________ > R-help@stat.math.ethz.ch mailing list > https://stat.ethz.ch/mailman/listinfo/r-help > PLEASE do read the posting guide > http://www.R-project.org/posting-guide.html > and provide commented, minimal, self-contained, > reproducible code. >Be smarter than spam. See how smart SpamGuard is at giving junk email the boot with the All-new Yahoo! Mail at http://mrd.mail.yahoo.com/try_beta?.intl=ca ------------------------------------------------------------------------------------------------------------------------ This communication is intended for the use of the recipient to which it is addressed, and may contain confidential, personal, and or privileged information. Please contact the sender immediately if you are not the intended recipient of this communication, and do not copy, distribute, or take action relying on it. Any communication received in error, or subsequent reply, should be deleted or destroyed. [[alternative HTML version deleted]]
Hi John, I figured out the skipping of variables using a first subset & then making a second subset & it worked! Thanks, Natalie __________________ You need to have a look at Chapter 5 of the Intro to R. I would recommend downloading the pdf and printing it out. It is not an easy read but it should help. newdata <- mydata[, c(PUMDID, Y_Q10A)] or newdata <- mydata[, c(1,33)] should do the trick --- Natalie O'Toole <notoole@mtroyal.ca> wrote:> Hi, > > Does anyone know how to skip variables (or columns) > in R. Say, for example > i had PUMFID position1 and Y_Q10A position 33 and i > do not want to include > all the variables in between. Is there a way to do > this in R when you are > extracting variables from a large .txt file with > many, many variables? > > Thanks, > > Nat > > __________________ > > > Yes but I believe it will vary depending on what > package you're using. I don't deal with weigthed > data > so I'm not a good source > > Have a look at help for something like lm in the > stats > package (part of the base installation) for an > example. > > ?lm > > weight is the fourth argument down. > > However for more information try > http://finzi.psych.upenn.edu/search.html and type in > weight. > > As Brian Ripley says in a reply to a question about > weights: > "Almost all methods I know of do: logistic > regression, neural nets, classification trees, PPR > .... " > > > --- Natalie O'Toole <notoole@mtroyal.ca> wrote: > > > Hi, > > > > Thank-you for the response!! That worked great!! > Is > > there any way to apply > > a weight variable to your file similar to what you > > can do in SPSS? So that > > all of your other variables will be weighted by > the > > weight variable? > > > > Thanks, > > > > Nat > > > > __________________ > > > > > > 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 > > > > 2)I cannot seem to skip variables properly when i > > choose certain other > > variables > > > > 3)i cannot get the combination of Select If > > statements to work to produce > > a different table with my new criteria > > > > 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 > > > > >------------------------------------------------------------------------------------------------------------------------> > > > > > This communication is intended for the use of the > > recipient to which it is > > addressed, and may > > contain confidential, personal, and or privileged > > information. Please > > contact the sender > > immediately if you are not the intended recipient > of > > this communication, > > and do not copy, > > distribute, or take action relying on it. Any > > communication received in > > error, or subsequent > > reply, should be deleted or destroyed. > > >------------------------------------------------------------------------------------------------------------------------> > > > > > This communication is intended for the use of the > > recipient to which it is > > addressed, and may > > contain confidential, personal, and or privileged > > information. Please > > contact the sender > > immediately if you are not the intended recipient > of > > this communication, > > and do not copy, > > distribute, or take action relying on it. Any > > communication received in > > error, or subsequent > > reply, should be deleted or destroyed. > > [[alternative HTML version > deleted]] > > > > ______________________________________________ > > R-help@stat.math.ethz.ch mailing list > > https://stat.ethz.ch/mailman/listinfo/r-help > > PLEASE do read the posting guide > > http://www.R-project.org/posting-guide.html > > and provide commented, minimal, self-contained, > > reproducible code. > > > > > > Be smarter than spam. See how smart SpamGuard > is at giving junk > email the boot with the All-new Yahoo! Mail at > http://mrd.mail.yahoo.com/try_beta?.intl=ca > > >------------------------------------------------------------------------------------------------------------------------> > > This communication is intended for the use of the > recipient to which it is > addressed, and may > contain confidential, personal, and or privileged > information. Please > contact the sender > immediately if you are not the intended recipient of > this communication, >=== message truncated == Be smarter than spam. See how smart SpamGuard is at giving junk email the boot with the All-new Yahoo! Mail at http://mrd.mail.yahoo.com/try_beta?.intl=ca ------------------------------------------------------------------------------------------------------------------------ This communication is intended for the use of the recipient to which it is addressed, and may contain confidential, personal, and or privileged information. Please contact the sender immediately if you are not the intended recipient of this communication, and do not copy, distribute, or take action relying on it. Any communication received in error, or subsequent reply, should be deleted or destroyed. [[alternative HTML version deleted]]