Append the tabelename to a variable name in a table

I have a table T. There are variables a,b,... in T. I would like to rename variables so that their names will be T_a,T_b,...etc. Please advise.

15 commentaires

T is not a file name, it's the table name. It's possible that it is also the name of a file which was used to generate the table but you're going to confuse people if you use file name when talking about table name.
What would be the purpose of adding redudant information to the the variable names? My first reflex is to say: don't do that!
alpedhuez
alpedhuez le 14 Juin 2018
Modifié(e) : alpedhuez le 14 Juin 2018
Suppose there are two tables NY and SF. Suppose each table has same variables such as population, area, etc. Then would be natural to have NY_pop, SF_pop, etc.
Not for me it wouldn't be natural. What I would do is merge the two tables into one adding one column containing the city.
Adding metadata to variable/column names is never a good idea. The metadata belongs into a variable/column so that it can be easily filtered against if needed.
With your system, if you want to get the total population of each city you end up having to loop over table names, with my system:
total_city_pop = rowfun(@sum, all_cities_table, 'GroupBy', 'City', 'InputVariables', 'population);
is all that is needed.
Stephen23
Stephen23 le 14 Juin 2018
Modifié(e) : Stephen23 le 14 Juin 2018
"Then would be natural to have NY_pop, SF_pop, etc."
No. This will make processing your data much more difficult. If you have some data that depends on the city, then add a city column to your table. Forcing meta-data into column names will make those columns hard to process because you then have to separate the meta-data from the actual column names, even though the actual quantities being measured are exactly the same metrics (e.g. area, population). Your would have to write pointlessly complex code to extract the meta-data and then process your table columns.
What you are proposing defeats the purpose of using tables, because then you cannot use any of the table tools (e.g. rowfun) for comparing your populations, areas, etc. Add a city variable and you will be able to actually use tables to compare and process your data (see Guillaume's example above).
Putting meta-data into variable names (i.e. the table names) is also a bad practice, for reasons that are discussed here:
You should not have separate tables with special names that contain meta-data: put all of your data into one table, or put multiple tables into a cell array. But do not have lots of tables with special names. Meta-data is data, so you need to treat it just like the rest of your data.
To add to Stephen's and my comment, your premise is already wrong. You shouldn't have two tables NY and SF. As soon as you start naming variables in some sort of sequence (numbers, city names, alphabetical, etc.) you've gone wrong. That sequencing doesn't belong in the variable name. It belongs to the variable content. Again, so that you can filter/aggregate the data programatically.
alpedhuez
alpedhuez le 14 Juin 2018
Modifié(e) : alpedhuez le 14 Juin 2018
"You should not have separate tables with special names that contain meta-data: put all of your data into one table, or put multiple tables into a cell array." True. Then how one is supposed to distinguish NY Pop from SF Pop? Just asking.
Stephen23
Stephen23 le 14 Juin 2018
Modifié(e) : Stephen23 le 14 Juin 2018
"Then how one is supposed to distinguish NY Pop from SF Pop?"
By the "City" column/variable (that you need to add to your table). Just like Guillaume and I already wrote.
As I said, add a column/variable wiith the city name:
>>T
City Year Population Area
---- ---- ---------- ----
NY 1990 111101 798
NY 1991 122232 800
SF 1990 78561 650
NY 1992 135410 810
SF 1991 85121 660
Suppose I have NY file and SF file with rows year and columns Pop and Area. I thought simplest would be to join the table.
Stephen23
Stephen23 le 14 Juin 2018
Modifié(e) : Stephen23 le 14 Juin 2018
"I thought simplest would be to join the table."
Sure, joining the tables is a very good idea. First add a "City" column, then join the tables together.
There are no entries of city name in table variables. In this case, it seems that one needs to get the table name ('city') one way or the other. I would try to put a sample table in future.
Stephen23
Stephen23 le 14 Juin 2018
Modifié(e) : Stephen23 le 14 Juin 2018
"There are no entries of city name in table variables"
We know that. That is why we advised you to add a new variable named "City".
"it seems that one needs to get the table name ('city') one way or the other"
Not true. Only if you want to write ugly, slow, buggy code that is hard to debug. You should just avoid this whole situation by NOT putting the city names into the table names in the first place. How did they get there? However you created those tables (by hand, importing from file,...) is the correct place to improve your code by NOT putting the city names into the table name, e.g. by loading the tables into a cell array, then you can trivially loop over the cells and add a column called "City". That is the simplest and best solution.
Magically accessing table names will force you into writing complex, buggy, ugly code. Avoid doing this.
But then what will be a simple way to write the code for what you have said?
Stephen23
Stephen23 le 14 Juin 2018
Modifié(e) : Stephen23 le 14 Juin 2018
@alpedhuez: here is some pseudocode. Adjust to suit your situation:
S = dir(...)
T = readtable(S(1).name)
T.City = ... cityname: from filename ???
for k = 2:numel(S)
U = readtable(S(k).name)
U.City = ... cityname: from filename ???
T = [T;U]
end
This is untested, it is just to illustrate one way to import multiple tables so that you do not need to magically access the table names, and can simply include the city name as a column in the table.
Let me work on it.

Connectez-vous pour commenter.

Réponses (1)

Steven Lord
Steven Lord le 14 Juin 2018
Modifié(e) : Steven Lord le 14 Juin 2018
Have a variable in the combined table named City. Rows in the combined table that came from your New York table would have NY in that variable, and those that came from your San Francisco table would have SF in that variable. Once you've added that variable to each of your tables, join them together using City as your key variable.
Once you have that as a variable in your table you could use it as the grouping variable in calls to groupsummary if you want to compute, say, the total population for all the regions / boroughs / etc. in your table that are in NY.
mytable = table({'NY'; 'SF'; 'NY'; 'NY'; 'SF'}, [1; 2; 3; 4; 5], ...
'VariableNames', {'City', 'pop'})
totalPopulation = groupsummary(mytable, 'City', 'sum', 'pop')

Catégories

Produits

Tags

Question posée :

le 14 Juin 2018

Commenté :

le 7 Juil 2018

Community Treasure Hunt

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

Start Hunting!

Translated by