rnorouzian
rnorouzian

Reputation: 7517

Adding elements of tables or arrays of tables to a dataframe in R

I was wondering how I could add elements of fpc (table object) in my code below as new column to the relevant rows of out?

By relevant rows of out, I mean rows that match the fpc table names. For example, for all rows in out where there is an F and an High, fpc will be 0.02027469.

Or for all rows in out where there is an M and an Medium, fpc will be 0.01984979, and so on.

Is this possible in R?

Note: This is a toy data. fpc could be an array of tables as well (see below). So, a functional solution is appreciated.

d <- read.csv('https://raw.githubusercontent.com/rnorouzian/d/master/su.csv')

out <- read.csv('https://raw.githubusercontent.com/rnorouzian/d/master/out.csv')

vars <- c("gender", "pre")

tt1 <- table(d[vars])

tt2 <- table(out[vars])

( fpc <- tt2/tt1 )

     pre
gender       High        Low     Medium
     F 0.02027469 0.01974522 0.02009274
     M 0.02014295 0.01991008 0.01984979


########## PLEASE NOTE: `fpc` could be an array of tables as shown below:

vars <- c("gender", "pre", "sector")

tt11 <- table(d[vars])

tt22 <- table(out[vars])

( fpc2 <- tt22/tt11 )

Upvotes: 1

Views: 234

Answers (2)

akrun
akrun

Reputation: 887118

Here, we just need to coerce the table to data.frame with as.data.frame, it would work with 2d and 3d arrays as well

out1 <- merge(out, as.data.frame(fpc2), all.x = TRUE)
names(out1)[names(out1)== "Freq"] <- "fpc"

With the updated case

 l1 <- length(dimnames(fpc))
 nm1 <- names(dimnames(fpc))
 if(l1 == 1 &&  nm1 == "") {
      names(dimnames(fpc)) <- "cname" # change here
   }

Now, we do the merge

out2 <- merge(out, as.data.frame(fpc))
identical(nrow(out2), nrow(out))
#[1] TRUE

Upvotes: 1

Duck
Duck

Reputation: 39595

Maybe are you looking for this (It is not clear for me what do you refer as an array? Is it a list of different fcp dataframes?):

library(tidyverse)
#Data
d <- read.csv('https://raw.githubusercontent.com/rnorouzian/d/master/su.csv')

out <- read.csv('https://raw.githubusercontent.com/rnorouzian/d/master/out.csv')

tt1 <- table(d[c("gender", "pre")])

tt2 <- table(out[c("gender", "pre")])

#Keys
fpc <- as.data.frame.matrix(tt2/tt1)
#Code
out2 <- out %>% left_join(fpc %>% rownames_to_column('gender') %>%
  pivot_longer(-gender) %>% rename(pre=name))

Output:

head(out2)
  no. fake.name  sector pretest state gender  pre          email       phone      value
1   1      Pont Private    1352    NY      F High    [email protected] xxx-xx-6216 0.02027469
2   2    Street     NGO    1438    CA      F High  [email protected] xxx-xx-6405 0.02027469
3   3    Galvan Private    1389    NY      F High  [email protected] xxx-xx-9195 0.02027469
4   4    Gorman     NGO    1375    CA      F High  [email protected] xxx-xx-1845 0.02027469
5   5   Jacinto Private    1386    CA      F High [email protected] xxx-xx-6237 0.02027469
6   6      Shah  Public    1384    CA      F High    [email protected] xxx-xx-5723 0.02027469

For multiple tables in an array, I would suggest working with dataframes to obtain the same results as table():

#Multiple tables
k1 <- d %>% group_by(sector,gender,pre) %>% summarise(N=n())
k2 <- out %>% group_by(sector,gender,pre) %>% summarise(N1=n())
k3 <- k1 %>% left_join(k2) %>% mutate(Value=N1/N) %>% select(-c(N,N1))
#Code
out3 <- out %>% left_join(k3)

Output:

head(out3)
  no. fake.name  sector pretest state gender  pre          email       phone      Value
1   1      Pont Private    1352    NY      F High    [email protected] xxx-xx-6216 0.01666667
2   2    Street     NGO    1438    CA      F High  [email protected] xxx-xx-6405 0.02236422
3   3    Galvan Private    1389    NY      F High  [email protected] xxx-xx-9195 0.01666667
4   4    Gorman     NGO    1375    CA      F High  [email protected] xxx-xx-1845 0.02236422
5   5   Jacinto Private    1386    CA      F High [email protected] xxx-xx-6237 0.01666667
6   6      Shah  Public    1384    CA      F High    [email protected] xxx-xx-5723 0.01067616

It is more practical having dataframes and then merge them.

Upvotes: 0

Related Questions