Forum Replies Created
- AuthorPosts
- November 17, 2014 at 4:23 pm in reply to: applying if then else logic to a column in a data frame #1077
bryan
ParticipantAlso, as I look at your code again, I’m not clear how the groups should be defined in your DOLGX objects. If you’re just trying to classify based on ranges (not specific discrete values), you can set your ifelses up as so:
DOL.group <- ifelse(Clean.Data$DOL >= .005 & Clean.Data$DOL <= .01 , DOL.Group = 1, NA), etc.
November 17, 2014 at 4:17 pm in reply to: applying if then else logic to a column in a data frame #1076bryan
ParticipantHi sander,
Welcome to R and ProgrammingR.com. A couple of suggestions:
1) Directly related to your question: use multiple ifelse() functions to create your groups. E.g,
DOL.group <- ifelse(Clean.Data$DOL %in% DOLG1, DOL.Group = 1, NA) DOL.group <- ifelse(Clean.Data$DOL %in% DOLG2, DOL.Group = 2, DOL.Group) DOL.group <- ifelse(Clean.Data$DOL %in% DOLG3, DOL.Group = 3, DOL.Group) Since ifelse() operates on vectors, you do not need to iterate through each row with a loop. There are other ways to do what you're asking, but this is a fine, straightforward way to handle it. 2) It's almost always a good idea to include stringsAsFators = 'FALSE' in your import command. It usually makes your data manipulations easier and less error prone, unless you are comfortable working with factors. 3) attach() can be more trouble than it is worth most of the time. I recommend against using it. I suspect 1) will fix your issue, but let us know if not. Bryan
bryan
ParticipantHi Carolina,
Since the parameters for the text function in your code are specified by position, the piece that’s directly causing that error is this:
paste(firstdiff,lastdiff,sep=”-”)
Check that you have what you expect in the firstdiff and lastdiff objects.
Bryan
bryan
ParticipantGlad you got it figured out. The stringAsFactors=TRUE is one of the most inexplicable defaults I’ve encountered.
Here’s an interesting, short and relevant Stack Overflow question:
bryan
ParticipantHi Sarah,
Can you provide a little more information, for example, the output from str(your.matrix) and your code?
Bryan
bryan
ParticipantThat one you’ll have to purchase, but it is worth the money in my opinion. Everything else on those lists is a web page or (free) downloadable PDF.
bryan
ParticipantRahul,
For true novices, I recommend a book I reviewed about a year ago. It is a “for Dummies” book, but as I mention in my review, it covers the basics and several important advanced topics. The review is posted here:
https://www.programmingr.com/content/r-for-dummies-de-vries-and-meys-2012/
Beyond that, there are many, many free resources on the web. Here are some that are either “required reading” (e.g., R Inferno) or otherwise good, concise collections. The first link covers primarily statistics topics, the second covers R programming.
https://www.programmingr.com/content/helpful-statistical-references/
https://www.programmingr.com/content/online-r-programming-resources/
Best of luck, and don’t hesitate to return with questions as you progress.
Bryan
bryan
ParticipantHi,
Welcome to ProgrammingR.com and R in general. R programming is a very marketable skill these days. Not only that, but learning R would likely help you with your analytic skills, in general. Are you looking for resources for learning R or just some thoughts on whether it is worthwhile for your situation?
Regards,
BryanNovember 20, 2013 at 8:40 pm in reply to: create dummy – convert continuous variable into (binary variable) using median #1014bryan
ParticipantHi tovy,
The R idiomatic way to do this is to use an ifelse() statement:
Course$grade2 <- ifelse(Course$grade >= median(Course$grade), 1, 0)
For more information on ifelse() you can type ?ifelse at a prompt or just ask here.
HTH,
Bryan- This reply was modified 11 years, 5 months ago by
bryan. Reason: typo
bryan
ParticipantHi Chai,
This is a path issue. My guess is you want something like:
write(data7, file = ‘~/Desktop/data7.txt’, sep = ‘,’)
Note the added tilde and forward slash.
Just repost if that doesn’t help.
- This reply was modified 11 years, 10 months ago by
bryan.
bryan
ParticipantHi Joepaul,
Yes, the pdf call is likely the problem. Try:
pdf(paste(x, ".pdf", sep=''))
Technically, you don’t need the extra counter (e.g., x) Just use your iterator variable/index:
pdf(paste(i, ".pdf", sep=''))
bryan
ParticipantThat’s definitely a task we run into often; anything to make that easier is helpful.
bryan
Participantbryan
ParticipantThat’s true, the base summary function will work. I’ve gotten in the habit of using the doBy package because it has a few other functions that are helpful, too, but for a simple grouping what you have is fine.
Do you mean you want to select cases where TZE_D_LOK begins with “^C51”, such as “^C51A”, “^C51B”, “^C51C”…?
In that case you can use grep. This is untested, but something like this should work:
summary(TZE_D_LOK, data = daten[grepl(pattern="^C51", x=daten$TZE_D_LOK, fixed=TRUE),], FUN = length)
bryan
ParticipantKeep in mind that R is case sensitive – I think your primary problem is that you’ve capitalized the “D” and not the “B” in “doBy”. But the warnings suggest that you might be having trouble with your internet connection as well. Checking/fixing those two things should fix it.
- This reply was modified 11 years, 5 months ago by
- AuthorPosts