bindData: Bind two data frames into a multivariate data frame

Description Usage Arguments Details Value Author(s) See Also Examples

View source: R/bindData.R

Description

Usually data frames represent one set of variables and one needs to bind/join them for multivariate analysis. When merge is not the approriate solution, bindData might perform an appropriate binding for two data frames. This is especially usefull when some variables are measured once, while others are repeated.

Usage

1
  bindData(x, y, common)

Arguments

x

data.frame

y

data.frame

common

character, list of column names that are common to both input data frames

Details

Data frames are joined in a such a way, that the new data frame has c + (n_1 - c) + (n_2 - c) columns, where c is the number of common columns, and n_1 and n_2 are the number of columns in the first and in the second data frame, respectively.

Value

A data frame.

Author(s)

Gregor Grojanc

See Also

merge, wideByFactor

Examples

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
n1 <- 6
n2 <- 12
n3 <- 4
## Single trait 1
num <- c(5:n1, 10:13)
(tmp1 <- data.frame(y1=rnorm(n=n1),
                    f1=factor(rep(c("A", "B"), n1/2)),
                    ch=letters[num],
                    fa=factor(letters[num]),
                    nu=(num) + 0.5,
                    id=factor(num), stringsAsFactors=FALSE))

## Single trait 2 with repeated records, some subjects also in tmp1 
num <- 4:9
(tmp2 <- data.frame(y2=rnorm(n=n2),
                    f2=factor(rep(c("C", "D"), n2/2)),
                    ch=letters[rep(num, times=2)],
                    fa=factor(letters[rep(c(num), times=2)]),
                    nu=c((num) + 0.5, (num) + 0.25),
                    id=factor(rep(num, times=2)), stringsAsFactors=FALSE))

## Single trait 3 with completely distinct set of subjects
num <- 1:4
(tmp3 <- data.frame(y3=rnorm(n=n3),
                    f3=factor(rep(c("E", "F"), n3/2)),
                    ch=letters[num],
                    fa=factor(letters[num]),
                    nu=(num) + 0.5,
                    id=factor(num), stringsAsFactors=FALSE))

## Combine all datasets
(tmp12 <- bindData(x=tmp1, y=tmp2, common=c("id", "nu", "ch", "fa")))
(tmp123 <- bindData(x=tmp12, y=tmp3, common=c("id", "nu", "ch", "fa")))

## Sort by subject
tmp123[order(tmp123$ch), ]

Example output

sh: 1: cannot create /dev/null: Permission denied
gdata: Unable to locate valid perl interpreter
gdata: 
gdata: read.xls() will be unable to read Excel XLS and XLSX files
gdata: unless the 'perl=' argument is used to specify the location of a
gdata: valid perl intrpreter.
gdata: 
gdata: (To avoid display of this message in the future, please ensure
gdata: perl is installed and available on the executable search path.)
sh: 1: cannot create /dev/null: Permission denied
gdata: Unable to load perl libaries needed by read.xls()
gdata: to support 'XLX' (Excel 97-2004) files.

gdata: Unable to load perl libaries needed by read.xls()
gdata: to support 'XLSX' (Excel 2007+) files.

gdata: Run the function 'installXLSXsupport()'
gdata: to automatically download and install the perl
gdata: libaries needed to support Excel XLS and XLSX formats.

Attaching package: 'gdata'

The following object is masked from 'package:stats':

    nobs

The following object is masked from 'package:utils':

    object.size

The following object is masked from 'package:base':

    startsWith

           y1 f1 ch fa   nu id
1 -0.05664323  A  e  e  5.5  5
2  0.25359420  B  f  f  6.5  6
3  0.32733960  A  j  j 10.5 10
4  1.01989860  B  k  k 11.5 11
5 -2.05107601  A  l  l 12.5 12
6  0.54341547  B  m  m 13.5 13
            y2 f2 ch fa   nu id
1  -1.02105241  C  d  d 4.50  4
2  -0.46420469  D  e  e 5.50  5
3  -0.14824942  C  f  f 6.50  6
4   1.50698463  D  g  g 7.50  7
5  -1.45749130  C  h  h 8.50  8
6   0.75847306  D  i  i 9.50  9
7  -0.37763854  C  d  d 4.25  4
8  -0.71337112  D  e  e 5.25  5
9   0.80236197  C  f  f 6.25  6
10  0.81143497  D  g  g 7.25  7
11  1.39475062  C  h  h 8.25  8
12 -0.04810913  D  i  i 9.25  9
          y3 f3 ch fa  nu id
1  0.4312451  E  a  a 1.5  1
2 -0.5687220  F  b  b 2.5  2
3 -2.4601100  E  c  c 3.5  3
4 -0.1766694  F  d  d 4.5  4
   id    nu ch fa          y1   f1          y2   f2
1   5  5.50  e  e -0.05664323    A          NA <NA>
2   6  6.50  f  f  0.25359420    B          NA <NA>
3  10 10.50  j  j  0.32733960    A          NA <NA>
4  11 11.50  k  k  1.01989860    B          NA <NA>
5  12 12.50  l  l -2.05107601    A          NA <NA>
6  13 13.50  m  m  0.54341547    B          NA <NA>
7   4  4.50  d  d          NA <NA> -1.02105241    C
8   5  5.50  e  e          NA <NA> -0.46420469    D
9   6  6.50  f  f          NA <NA> -0.14824942    C
10  7  7.50  g  g          NA <NA>  1.50698463    D
11  8  8.50  h  h          NA <NA> -1.45749130    C
12  9  9.50  i  i          NA <NA>  0.75847306    D
13  4  4.25  d  d          NA <NA> -0.37763854    C
14  5  5.25  e  e          NA <NA> -0.71337112    D
15  6  6.25  f  f          NA <NA>  0.80236197    C
16  7  7.25  g  g          NA <NA>  0.81143497    D
17  8  8.25  h  h          NA <NA>  1.39475062    C
18  9  9.25  i  i          NA <NA> -0.04810913    D
   id    nu ch fa          y1   f1          y2   f2         y3   f3
1   5  5.50  e  e -0.05664323    A          NA <NA>         NA <NA>
2   6  6.50  f  f  0.25359420    B          NA <NA>         NA <NA>
3  10 10.50  j  j  0.32733960    A          NA <NA>         NA <NA>
4  11 11.50  k  k  1.01989860    B          NA <NA>         NA <NA>
5  12 12.50  l  l -2.05107601    A          NA <NA>         NA <NA>
6  13 13.50  m  m  0.54341547    B          NA <NA>         NA <NA>
7   4  4.50  d  d          NA <NA> -1.02105241    C         NA <NA>
8   5  5.50  e  e          NA <NA> -0.46420469    D         NA <NA>
9   6  6.50  f  f          NA <NA> -0.14824942    C         NA <NA>
10  7  7.50  g  g          NA <NA>  1.50698463    D         NA <NA>
11  8  8.50  h  h          NA <NA> -1.45749130    C         NA <NA>
12  9  9.50  i  i          NA <NA>  0.75847306    D         NA <NA>
13  4  4.25  d  d          NA <NA> -0.37763854    C         NA <NA>
14  5  5.25  e  e          NA <NA> -0.71337112    D         NA <NA>
15  6  6.25  f  f          NA <NA>  0.80236197    C         NA <NA>
16  7  7.25  g  g          NA <NA>  0.81143497    D         NA <NA>
17  8  8.25  h  h          NA <NA>  1.39475062    C         NA <NA>
18  9  9.25  i  i          NA <NA> -0.04810913    D         NA <NA>
19  1  1.50  a  a          NA <NA>          NA <NA>  0.4312451    E
20  2  2.50  b  b          NA <NA>          NA <NA> -0.5687220    F
21  3  3.50  c  c          NA <NA>          NA <NA> -2.4601100    E
22  4  4.50  d  d          NA <NA>          NA <NA> -0.1766694    F
   id    nu ch fa          y1   f1          y2   f2         y3   f3
19  1  1.50  a  a          NA <NA>          NA <NA>  0.4312451    E
20  2  2.50  b  b          NA <NA>          NA <NA> -0.5687220    F
21  3  3.50  c  c          NA <NA>          NA <NA> -2.4601100    E
7   4  4.50  d  d          NA <NA> -1.02105241    C         NA <NA>
13  4  4.25  d  d          NA <NA> -0.37763854    C         NA <NA>
22  4  4.50  d  d          NA <NA>          NA <NA> -0.1766694    F
1   5  5.50  e  e -0.05664323    A          NA <NA>         NA <NA>
8   5  5.50  e  e          NA <NA> -0.46420469    D         NA <NA>
14  5  5.25  e  e          NA <NA> -0.71337112    D         NA <NA>
2   6  6.50  f  f  0.25359420    B          NA <NA>         NA <NA>
9   6  6.50  f  f          NA <NA> -0.14824942    C         NA <NA>
15  6  6.25  f  f          NA <NA>  0.80236197    C         NA <NA>
10  7  7.50  g  g          NA <NA>  1.50698463    D         NA <NA>
16  7  7.25  g  g          NA <NA>  0.81143497    D         NA <NA>
11  8  8.50  h  h          NA <NA> -1.45749130    C         NA <NA>
17  8  8.25  h  h          NA <NA>  1.39475062    C         NA <NA>
12  9  9.50  i  i          NA <NA>  0.75847306    D         NA <NA>
18  9  9.25  i  i          NA <NA> -0.04810913    D         NA <NA>
3  10 10.50  j  j  0.32733960    A          NA <NA>         NA <NA>
4  11 11.50  k  k  1.01989860    B          NA <NA>         NA <NA>
5  12 12.50  l  l -2.05107601    A          NA <NA>         NA <NA>
6  13 13.50  m  m  0.54341547    B          NA <NA>         NA <NA>

gdata documentation built on May 2, 2019, 5:49 p.m.