R programming r bind error

0 votes

My R function does the following:

  • reads in the directory name 
  • reads a particular set of numbers that represents the files
  • combines the mentioned files into a data frame
  • prints out the number of occurrences for a particular ID

Here's the function:

library(plyr)

complete <- function(directory, id=1:5){

  files_full <- list.files(directory, full.names=TRUE)
  working_set <- files_full [id]
  seethis <- lapply(working_set, read.csv)
  output <- do.call(rbind, seethis)

  no_na <- na.omit(output)
  new_df <- as.data.frame(no_na)
  new_output <- count(new_df,"ID")
  colnames(new_output) <- c("id", "Occurrences")
  new_output
}

Now, If the function is invoked with

complete("diet_data",c(4,3,2))

I get the following:

  id Occurrences
1  2          30
2  3          17
3  4          30

But I wish to retain the id order, i.e. 4, 3, 2 so that output is

  id Occurrences
1  4          30
2  3          17
3  2          30

How to do this? And there is no way to tell if id will be 1:5, 5:3, etc

Apr 3, 2019 in Data Analytics by Sophie may
• 10,610 points
649 views

2 answers to this question.

0 votes

Add this line at the end of your R function:

sorted_output <- new_output[match(id, new_output$ID),]
answered Apr 3, 2019 by Tyrion anex
• 8,700 points
0 votes

You can use arrange function at last line like below.

new_output %>% arrange(-id)
answered Sep 3, 2019 by anonymous
• 33,030 points

Related Questions In Data Analytics

+3 votes
2 answers

Error: could not find function - R Programming

Yes, Just like @Maverik said, It happens ...READ MORE

answered Aug 23, 2019 in Data Analytics by anonymous
• 33,030 points
32,828 views
0 votes
1 answer

"no applicable method" Error in r programming

This is caused by using an object-oriented ...READ MORE

answered Oct 31, 2018 in Data Analytics by Kalgi
• 52,360 points
2,228 views
+2 votes
1 answer

“subscript out of bounds” Error in r programming

This error is likely to occur when ...READ MORE

answered Oct 31, 2018 in Data Analytics by Kalgi
• 52,360 points
9,351 views
0 votes
2 answers

R Programming error in if condition

Hi. Instead of breaking the line add it ...READ MORE

answered Aug 26, 2019 in Data Analytics by anonymous
• 33,030 points
722 views
0 votes
1 answer

R programming: Unexpected symbol error

Format your code this way: myfunction <- function() ...READ MORE

answered Dec 17, 2018 in Data Analytics by Sophie may
• 10,610 points
2,816 views
0 votes
1 answer

R Programming error in 'fert'

You're using a factor: fert <- factor(c(50,20,10,10,20,50)) levels(fert) #[1] ...READ MORE

answered Dec 28, 2018 in Data Analytics by Sophie may
• 10,610 points
447 views
+1 vote
1 answer

R Programming: Market Basket Analysis Error

The basket.sorted() has less than 5 rules. Refer ...READ MORE

answered Feb 12, 2019 in Data Analytics by Sophie may
• 10,610 points
1,205 views
+1 vote
1 answer

R Programming: regexpr error

The below code will help: gregexpr("D", x) # [[1]] # ...READ MORE

answered Feb 21, 2019 in Data Analytics by Tyrion anex
• 8,700 points
367 views
+1 vote
1 answer

R programming error

Alright, you can either use gsub to match the ...READ MORE

answered Dec 18, 2018 in Data Analytics by Tyrion anex
• 8,700 points
436 views
0 votes
1 answer

R Programming error: twitteR OAuthFactory object

You need to install the following packages: install.packages(c('RO ...READ MORE

answered Jan 11, 2019 in Data Analytics by Tyrion anex
• 8,700 points
718 views
webinar REGISTER FOR FREE WEBINAR X
REGISTER NOW
webinar_success Thank you for registering Join Edureka Meetup community for 100+ Free Webinars each month JOIN MEETUP GROUP