[R] How to create a new column based on the values from multiple columns which are matching a particular string?
Eric Berger
er|cjberger @end|ng |rom gm@||@com
Mon Jul 29 21:38:32 CEST 2019
Read the help for apply and %in%
?apply
?%in%
Sent from my iPhone
> On 29 Jul 2019, at 22:23, Ana Marija <sokovic.anamarija using gmail.com> wrote:
>
> Thank you so much! Just to confirm here MARGIN=1 indicates that "A" should appear at least once per row?
>
>> On Mon, Jul 29, 2019 at 1:53 PM Eric Berger <ericjberger using gmail.com> wrote:
>> df$case <- apply(df,MARGIN = 1,function(v) { as.integer("A" %in% v) })
>>
>>
>>> On Mon, Jul 29, 2019 at 9:02 PM Ana Marija <sokovic.anamarija using gmail.com> wrote:
>>> sorry my bad, here is the edited version:
>>>
>>> so the data frame is this:
>>>
>>> df=data.frame(
>>> eye_problemsdisorders_f6148_0_1=c("A","C","D",NA,"D","A","C",NA,"B","A"),
>>> eye_problemsdisorders_f6148_0_2=c("B","C",NA,"A","C","B",NA,NA,"A","D"),
>>> eye_problemsdisorders_f6148_0_3=c("C","A","D","D","B","A",NA,NA,"A","B"),
>>> eye_problemsdisorders_f6148_0_4=c("D","D",NA,"B","A","C",NA,"C","A","B"),
>>> eye_problemsdisorders_f6148_0_5=c("C","C",NA,"D","B","C",NA,"D","D","B")
>>>
>>> and I would need to put inside the column which would be named "case" and values inside would be: 1,1,0,1,1,1,0,0,1,1
>>>
>>> so "case" column is where value "A" can be found in any column.
>>>
>>>> On Mon, Jul 29, 2019 at 12:53 PM Eric Berger <ericjberger using gmail.com> wrote:
>>>> You may have a typo/misstatement in your question.
>>>> You define a data frame with 5 columns, each of which has 10 elements, so your data frame has dimensions 10 x 5.
>>>> Then you request a new COLUMN which will have only 5 elements, which is not allowed. All columns of a data frame
>>>> must have the same length.
>>>>
>>>>> On Mon, Jul 29, 2019 at 8:42 PM Ana Marija <sokovic.anamarija using gmail.com> wrote:
>>>>> I have data frame which looks like this:
>>>>>
>>>>> df=data.frame(
>>>>> eye_problemsdisorders_f6148_0_1=c(A,C,D,NA,D,A,C,NA,B,A),
>>>>> eye_problemsdisorders_f6148_0_2=c(B,C,NA,A,C,B,NA,NA,A,D),
>>>>> eye_problemsdisorders_f6148_0_3=c(C,A,D,D,B,A,NA,NA,A,B),
>>>>> eye_problemsdisorders_f6148_0_4=c(D,D,NA,B,A,C,NA,C,A,B),
>>>>> eye_problemsdisorders_f6148_0_5=c(C,C,NA,D,B,C,NA,D,D,B))
>>>>>
>>>>> In reality I have much more columns and they don't always match
>>>>> "eye_problemsdisorders_f6148" this string, and there is much more rows.
>>>>>
>>>>> What I would like to do is create a new column, say named "case" where I
>>>>> would have value "1" for every row where string "A" appears at least once
>>>>> in any column, if not the value would be "0". So in the above example
>>>>> column "case" would have these values: 1,1,1,1,0
>>>>> Thanks
>>>>> Ana
>>>>>
>>>>> [[alternative HTML version deleted]]
>>>>>
>>>>> ______________________________________________
>>>>> R-help using r-project.org mailing list -- To UNSUBSCRIBE and more, see
>>>>> https://stat.ethz.ch/mailman/listinfo/r-help
>>>>> PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
>>>>> and provide commented, minimal, self-contained, reproducible code.
[[alternative HTML version deleted]]
More information about the R-help
mailing list