Create new column in table based on another column value
조회 수: 37 (최근 30일)
이전 댓글 표시
I have a table with the list of User IDs and I used "Groupummary" to determine the number of occurences of each ID. My table T:
ID Occurrences
123 9
345 1
234 3
I need to create another column in the table to mark the ID as "unique" or not based on the number of occurrences.
Expected output:
ID Occurrences Unique
123 9 No
345 1 Yes
234 3 No
I'm using the following code, but I get error saying the number of rows should match the length of table:
if T.Occurences> 1
T.Unique = "No";
else
T.Unique = "Yes";
end
How do I check the length of table and fill the "Unique" column for each corresponding cell?
댓글 수: 0
채택된 답변
Steven Lord
2020년 11월 26일
Let's start with a sample table.
T = table([123; 345; 234], [9; 1; 3], 'VariableNames', ["ID", "Occurrences"])
Fill in the Unique variable with a default value.
T.Unique = repmat("No", height(T), 1)
Change the value of the Unique variable based on the contents of the Occurrences variable.
T.Unique(T.Occurrences == 1) = "Yes"
추가 답변 (0개)
참고 항목
카테고리
Help Center 및 File Exchange에서 Tables에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!