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 2 in Data Analytics by Sophie may
• 9,870 points
42 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 2 by Tyrion anex
• 8,280 points
0 votes

You can use arrange function at last line like below.

new_output %>% arrange(-id)
answered Sep 3 by anonymous
• 31,840 points

Related Questions In Data Analytics

0 votes
2 answers

Error: could not find function - R Programming

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

answered Aug 22 in Data Analytics by anonymous
• 31,840 points
5,116 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
• 42,540 points
103 views
0 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
• 42,540 points
2,209 views
0 votes
2 answers

R Programming error in if condition

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

answered Aug 26 in Data Analytics by anonymous
• 31,840 points
64 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
• 9,870 points
313 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 27, 2018 in Data Analytics by Sophie may
• 9,870 points
53 views
0 votes
1 answer

R Programming: Market Basket Analysis Error

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

answered Feb 11 in Data Analytics by Sophie may
• 9,870 points
152 views
0 votes
1 answer

R Programming: regexpr error

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

answered Feb 21 in Data Analytics by Tyrion anex
• 8,280 points
32 views
0 votes
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,280 points
32 views
0 votes
1 answer

R Programming error: twitteR OAuthFactory object

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

answered Jan 11 in Data Analytics by Tyrion anex
• 8,280 points
95 views