In R: Repeats in Dataframes are Causing Returns to Change

Multi tool use


In R: Repeats in Dataframes are Causing Returns to Change
I have two dataframes that I am trying to pull from to create a new dataframe.
DF1 DF2
ClaimID Money Type ClaimID Money Type
1 500 "Weather" 1 500 "Non-Weather"
1 200 "Weather" 1 200 "Non-Weather"
2 50 "Non-Weather" 2 50 "Non-Weather"
Using this code:
DF3<-data.frame("ClaimID" = DF1$ClaimID, "FinalType" =
DF1$Type,"OldType" = DF2$Type)
With this code, adding a new column to show whether the "FinalType" and "OldType" agree:
DF3<-cbind(DF3, Agreement =c(ifelse(DF3$OldType ==
DF3$FinalType, "Agree","Disagree")))
I EXCPECT to create this dataframe:
DF3
ClaimID FinalType OldType Agreement
1 Weather Non-Weather Disagree
1 Weather Non-Weather Disagree
2 Non-Weather Non-Weather Agree
However, I am getting:
DF3
ClaimID FinalType OldType Agreement
1 Weather Non-Weather Disagree
1 Weather weather Agree
2 Non-Weather Non-Weather Agree
So, somehow it is changing the Type in DF2, even though in DF2, the type remains the same. Thank you
I'm sorry, I don't understand what exactly you want. I can put in my exact codes but I do not think they would help to recreate without making the question very complex. The dataframes are about 20,000 obs long.
– Reagan
48 mins ago
1 Answer
1
The following code is a simplified example of what you want.
df1 <- data.frame(
claim = c(1, 1, 2),
type1 = c("w", "w", "n"),
stringsAsFactors = FALSE
)
df2 <- data.frame(
claim = c(1, 1, 2),
type2 = c("n", "n", "n"),
stringsAsFactors = FALSE
)
df <- cbind(df1, df2$type2)
df$test <- df$type1 == df$type2
Note that with R it's better practice to use code of the form a == b
rather than ifelse
to test (element-wise) whether two vectors are equal.
a == b
ifelse
In general, be careful when using cbind
! More often you will want to use merge
to combine dataframes.
cbind
merge
By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.
Can you please include a complete example which produces the bad output? When I build the tables and run your code as provided I get your expected output.
– Isaac
58 mins ago