creating a Dummy variable from a string vector
조회 수: 2 (최근 30일)
이전 댓글 표시
Hi, i want to create a dummy variable of a string columnn cointaining information over investors. I have identified the names of the investors that i demme to have experience and would like to create a dummy = 1 if the cell contain the name of said investor, and 0 if not. The dataset is quite large so cant do this manually, not really sure how to proceed.
댓글 수: 2
Walter Roberson
2022년 5월 13일
When you say "contains" do you mean that you want to compare the entries for exact equality? Or should "sam" be considered to be contained in "assam" or "samuel"? Should the comparison be case sensitive? Should "Billie-Joe" be considered the same as "Billie Joe"?
답변 (2개)
Steven Lord
2022년 5월 13일
Use the string manipulation and/or set membership functions. Let's start with a random set of names:
rng default
D = ["Doc"; "Grumpy"; "Happy"; "Sleepy"; "Bashful"; "Sneezy"; "Dopey"];
randomDwarfs = D(randi(numel(D), [20 1]))
We can use matches to look for an exact match; startsWith, endsWith, or contains to look for text inside a name; or ismember to ask for members of a group of names.
onlyGrumpy = matches(randomDwarfs, "Grumpy");
startsWithD = startsWith(randomDwarfs, "D"); % Doc, Dopey
bashfulOrSleepy = ismember(randomDwarfs, ["Bashful"; "Sleepy"]);
results = table(randomDwarfs, onlyGrumpy, startsWithD, bashfulOrSleepy)
I can extract the names from randomDwarfs using those logical vectors.
BorS = randomDwarfs(bashfulOrSleepy)
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Logical에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!