0

I have an csv file and i want to get a specialized output with just typing in the ID (PIPAPNr) for a letter for example input = PIPAP1147

output Roger Nadal 11.07.1993

Pipapnr="PIPAP1147"
for (i in 1:nrow(Patienten)){
  if (Patienten$PIPAP.Nr.==Pipapnr)
    DOB<- (Patienten$Geburtsdatum[i])
    Name<- (Patienten$Name[i])}

The error is

In if (Patienten$PIPAP.Nr. == Pipapnr) DOB <- (Patienten$Geburtsdatum[i]) :
  the condition has length > 1 and only the first element will be used
1
  • Please provide a minimal working example WITH a few data records. Second, R has scores if not hundreds of specialized functions for all sorts of database operations in many formats. Writing your own for loops is time-consuming and inefficient as the builtin functions are much more optimized and already fully debugged (well mostly!). Start by examining the base R functions which() and unique(). In the end you are going to want to look at data tables, likely, if you're in a large setting. Commented May 16, 2022 at 12:08

1 Answer 1

1

In if (Patienten$PIPAP.Nr. == Pipapnr) DOB <- (Patienten$Geburtsdatum[i]) : the condition has length > 1 and only the first element will be used

In this code Pipapnr contains just one value, however Patienten$PIPAP.Nr. probably contains lots of values so there are many comparisons and only the first is used.

That is the explanation of the error message. Probably you wanted the ifclause to read as if (Patienten$PIPAP.Nr.[i]==Pipapnr) ...

Still, John Garland is right in his comment, that these things can be handled more elegantly in R. Maybe something like which(Patienten$PIPAP.Nr.==Pipapnr?

As your Code reads German, maybe you are interested in the German R forum at http://forum.r-statistik.de ?

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.