Reputation: 1829
I have a slightly complicated problem today. I have a data frame (df) with three columns:
ID Organization org
1 Company1 company1
2 Company1 company1
3 Company2 company2
4 Company2 company1
5 Company2 NA
6 Company3 company3
7 Company3 demo
8 Company4 company4
I want to create a new column called "org_corrected" with the following condition:
If entry in "Organization" and "org" is the same then "org_corrected" must be equal to entry in "Organization"
If entry in "Organization" and "org" is not same and "org" not equal to "demo" and "NA" then org_corrected" must have entry of org (first letter capitalize")
Expected output:
ID Organization org org_corrected
1 Company1 company1 Company1
2 Company1 company1 Company1
3 Company2 company2 Company2
4 Company2 company1 Company1
5 Company2 NA Company2
6 Company3 company3 Company3
7 Company3 demo Company3
8 Company4 company4 Company4
I can do this easily in excel, but want to do this in R.
Thanks in advance, you guys/girls have always solved my problems.
Upvotes: 1
Views: 78
Reputation: 887961
We can use ifelse
df1$org_corrected <- with(df1,
ifelse(toupper(Organization)==toupper(org)|is.na(org)|org=='demo',
Organization, org))
If we need to change the case of some entries 'org_correctedto
camelcase` (i.e. from 'org')
library(rapport)
df1$org_corrected <- tocamel(df1$org_corrected, upper=TRUE)
Upvotes: 1