replacedata

Class: dataset

Replace dataset variables

The dataset data type might be removed in a future release. To work with heterogeneous data, use the MATLAB® table data type instead. See MATLAB table documentation for more information.

Syntax

B = replacedata(A,X)
B = replacedata(A,X,vars)
B = replacedata(A,fun)
B = replacedata(A,fun,vars)

Description

B = replacedata(A,X) creates a dataset array B with the same variables as the dataset array A, but with the data for those variables replaced by the data in the array X. replacedata creates each variable in B using one or more columns from X, in order. X must have as many columns as the total number of columns in all of the variables in A, and as many rows as A has observations.

B = replacedata(A,X,vars) creates a dataset array B with the same variables as the dataset array A, but with the data for the variables specified in vars replaced by the data in the array X. The remaining variables in B are copies of the corresponding variables in A. vars is a positive integer, a vector of positive integers, a variable name, a cell array containing one or more variable names, or a logical vector. Each variable in B has as many columns as the corresponding variable in A. X must have as many columns as the total number of columns in all the variables specified in vars.

B = replacedata(A,fun) or B = replacedata(A,fun,vars) creates a dataset array B by applying the function fun to the values in A's variables. replacedata first horizontally concatenates A's variables into a single array, then applies the function fun. The specified variables in A must have types and sizes compatible with the concatenation. fun is a function handle that accepts a single input array and returns an array with the same number of rows and columns as the input.

Examples

data = dataset({rand(3,3),'Var1','Var2','Var3'})

% Use ZSCORE to normalize each variable in a dataset array
% separately, by explicitly extracting and transforming the
% data, and then replacing it.
X = double(data);
X = zscore(X);
data = replacedata(data,X)

% Equivalently, provide a handle to ZSCORE.
data = replacedata(data,@zscore)

% Use ZSCORE to normalize each observation in a dataset
% array separately by creating an anonymous function.
data = replacedata(data,@(x) zscore(x,[],2)) 

See Also

Was this topic helpful?