Filtering down a readtable based on specific value in a column

19 次查看(过去 30 天)
I have a running table of stats from a game with friends and I want to pull only the first place placements into a new table. Essentially, I want to take RawBonusStars(i,:), the entire ith row and place it into BonusStars(i,:). In this dropped row I would have 10 variables, (1 & 3:10) are just numbers or NaN and the 2nd column would be the players name.
This is what my current code looks like and it returns an error: "Conversion to double from cell is not possible"
stats = 'Mario party tracker.csv'
T = readtable(stats);
games = (unique(T.Game_));
rows = height(T)/4;
RawBonusStars = [T(:,game),(T(:,names)),T(:,placement),T(:,MiniGame),T(:,MaxCoin),T(:,Happening),T(:,Orb),T(:,Shopping),T(:,Running),T(:,RedSpaceStar)];
BonusStars = array2table(zeros(height(games),10));
for i=1:rows
if RawBonusStars.Placement(i) ==1
BonusStars(i,:) = RawBonusStars(i,:)
end
end
I tried to pin point the problem down and I suspect its due to the "name" row. Both RawBonusStars and BonusStars should be initalized as tables so im not sure what the issue is.
  6 个评论
Cris LaPierre
Cris LaPierre 2021-7-31
What exactly is the issue you are having? There are errors in the code well before the actual assignment operation.
Trevor Palmer
Trevor Palmer 2021-7-31
I am trying to take a table that is currently 248 x 10 and filter it down to a table that is 61 x 10 based on a value in the placements column. End goal is to have a table of only the first place placements and the bonus stars that person recieved for that specific game. But in trying to fill the smaller table im receiving a "Conversion to double from cell is not possible" error.

请先登录,再进行评论。

采纳的回答

Image Analyst
Image Analyst 2021-7-31
"take a table that is currently 248 x 10 and filter it down to a table that is 61 x 10" OK then did you want to filter based on Placement:
stats = 'Mario party tracker.csv'
T = readtable(stats);
rowsToExtract = T.Placement == 1;
T2 = T(rowsToExtract, :)
fprintf('Done running %s.m.\n', mfilename);

更多回答(2 个)

Image Analyst
Image Analyst 2021-7-31
Did you mean this?
RawBonusStars = table(T.Game_, T.Name,T.Placement,T.MinigameCoins, T.Coins,T.HappeningSpaces,T.Orbs,T.ShoppingMoney,T.Running,T.RedSpaces)
  2 个评论
Cris LaPierre
Cris LaPierre 2021-7-31
Could also be
RawBonusStars = T(:,["Game_","Name","Placement","MinigameCoins","MaxCoins","HappeningSpaces","Orbs","ShoppingMoney","Running","BonusRedStar"]);
but we are guessing what variables were intended to be included
Trevor Palmer
Trevor Palmer 2021-7-31
This line of code does the same as what I have but the table generated with this lacks the variable names.

请先登录,再进行评论。


Cris LaPierre
Cris LaPierre 2021-7-31
编辑:Cris LaPierre 2021-7-31
Assuming we are missing some of the code (all the column numbers used to construct RawBonusStars, I believe your issue with the actual assignment is a typo.
BonusStars(i,:) = RawBounsStars(i,:)
^^^ % should be RawBonusStars
  2 个评论
Trevor Palmer
Trevor Palmer 2021-7-31
That was just a typo when I wrote the code on here. Its spelled correctly in my code. I also editted the OP to correct for that.
Cris LaPierre
Cris LaPierre 2021-7-31
It looks like you had 62 games, not 61. Use logical indexing to extract all the first place finishers. No loop needed.
stats = 'Mario party tracker.csv'
stats = 'Mario party tracker.csv'
T = readtable(stats);
Warning: Column headers from the file were modified to make them valid MATLAB identifiers before creating variable names for the table. The original column headers are saved in the VariableDescriptions property.
Set 'VariableNamingRule' to 'preserve' to use the original column headers as table variable names.
RawBonusStars = ["Game_","Name","Placement","BonusMinigameStar","BonusMaxCoin","BonusHappening","BonusOrbStar","BonusShoppingStar","BonusRunnerStar","BonusRedStar"];
ind = T.Placement == 1;
BonusStars = T(ind,RawBonusStars)
BonusStars = 62×10 table
Game_ Name Placement BonusMinigameStar BonusMaxCoin BonusHappening BonusOrbStar BonusShoppingStar BonusRunnerStar BonusRedStar _____ __________ _________ _________________ ____________ ______________ ____________ _________________ _______________ ____________ 1 {'Juan' } 1 0 0 1 NaN NaN NaN NaN 2 {'Josh' } 1 0 1 0 NaN NaN NaN NaN 3 {'Trevor'} 1 0 1 1 NaN NaN NaN NaN 4 {'Josh' } 1 0 1 0 NaN NaN NaN NaN 5 {'Adrien'} 1 0 0 1 NaN NaN NaN NaN 6 {'Juan' } 1 1 0 1 NaN NaN NaN NaN 7 {'Trevor'} 1 0 1 0 NaN NaN NaN NaN 8 {'Josh' } 1 1 0 0 NaN NaN NaN NaN 9 {'Daphne'} 1 0 0 0 NaN NaN NaN NaN 10 {'Trevor'} 1 1 0 0 NaN NaN NaN NaN 11 {'Trevor'} 1 0 1 0 NaN NaN NaN NaN 12 {'Josh' } 1 1 0 0 NaN NaN NaN NaN 13 {'Trevor'} 1 1 1 0 NaN NaN NaN NaN 14 {'Trevor'} 1 1 1 1 NaN NaN NaN NaN 15 {'Josh' } 1 1 0 0 NaN NaN NaN NaN 16 {'Trevor'} 1 1 0 0 NaN NaN NaN NaN

请先登录,再进行评论。

类别

Help CenterFile Exchange 中查找有关 Word games 的更多信息

产品


版本

R2021a

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by