0

This is my code on R, and it keeps getting me the message

Error: unexpected '}' in " }"

when I try to run it. What is the problem? I checked the balance of the brackets and according to forums it should work.

n = 1 
while(n < 3) { 
    i = 2
    while (i < 17) {
        data_freq = data_pourcentage %>%
            filter(groupe = i & groupe = n) %>%
            mutate(pourcentage = sum(freq)) %>%
            mutate(pourcentage = freq / pourcentage)
        data_pourcentage = left_join(
            data_freq, 
            data_pourcentage, 
            by = c(
                "sujet", "groupe", "identification", 
                "cristallisation", "valence", "freq")
            )
        i = i + 1
    }
    n = n + 1
}
4
  • 1
    Can you give some sample data so we can actually run it? Commented Apr 11, 2017 at 11:27
  • 3
    The & in the filter command is the problem. Just with filter(groupe = i) all works fine. Do you mean filter(groupe= c(i,n))? Commented Apr 11, 2017 at 11:30
  • awesome that was the problem! you rock mate :D Commented Apr 11, 2017 at 11:46
  • I would also try and replace the nested while loop with a call to group_by. Commented Apr 11, 2017 at 12:34

1 Answer 1

1

The & in the filter command is the problem. Just with filter(groupe = i) all works fine. To combine more components use filter(groupe= c(i,n)).

n = 1 
while(n < 3) { 
    i = 2
    while (i < 17) {
        data_freq = data_pourcentage %>%
            filter(groupe = c(i,n)) %>%
            mutate(pourcentage = sum(freq)) %>%
            mutate(pourcentage = freq / pourcentage)
        data_pourcentage = left_join(
            data_freq, 
            data_pourcentage, 
            by = c("sujet", "groupe", "identification", 
                   "cristallisation", "valence", "freq")
            )
        i = i + 1
    }
    n = n + 1
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.