recode {psychTools}R Documentation

Recode or rearrange or reshape variables or values to new values

Description

Given a set of numeric codes, change their values to different values given a mapping function. Also included are the ability to reorder columns or to convert wide sets of columns to long form

Usage

rearrange(x,pattern)   #reorder the variables
wide2long(x,width, cname=NULL, idname = NULL, idvalues=NULL ,pattern=NULL) 
recode(x, where, isvalue, newvalue)  #recode text values to numeric values

Arguments

x

A matrix or data frame of numeric values

where

The column numbers to fix

isvalue

A vector of values to change

newvalue

A vector of the new values

pattern

column order of repeating patterns

width

width of long format

cname

Variable names of long format

idname

Name of first column

idvalues

Values to fill first column

Details

Three functions for basic recoding are included.

recode: Sometime, data are entered as levels in an incorrect order. Once converted to numeric values, this can lead to confusion. recoding of the data to the correct order is straightforward, if tedious.

rearrange: Another tedious problem is when the output of one function needs to be arranged for better data handling in subsequent function. Specify a pattern of choosing the new columns.

wide2long: And then, having rearranged the data, perhaps convert the file to long format.

Value

The reordered data

Note

Although perhaps useful, the recode function is definitely ugly code. For smaller data sets, the results from char2numeric back to the original will not work. char2numeric works column wise and orders the data in each column.

Author(s)

William Revelle

See Also

mlArrange in the psych package for a more general version of wide2long

Examples

x <- matrix(1:120,ncol=12) 
new <- rearrange(x,pattern = c(1,4, 7,10))
new 
long <- wide2long(x,width=3,pattern=c(1,4, 7,10))  #rearrange and then make wide


temp <- bfi[1:100,1:5]
isvalue <- 1:6
newvalue <- psych::cs(one,two,three,four,five,six)
newtemp <- recode(temp,1:5,isvalue,newvalue)
newtemp  #characters
temp.num <- psych::char2numeric(newtemp) #convert to numeric
temp.num  #notice the numerical values have changed
new.temp.num <- recode(temp.num, 1:5, isvalue=c(3,6,5,2,1,4), newvalue=1:6)
#note that because char2numeric works column wise, this will fail for small sets


[Package psychTools version 2.4.1 ]