Print varible names in table() with 2 binary variables in R - r

I'm sure I'll kick myself for not being able to figure this out, but when you have a table with 2 variables (i.e. cross-tab) and both are binary or otherwise have the same levels, how can you make R show which variable is displayed row-wise and which is column-wise?
For example:
> table(tc$tr, tc$fall_term)
0 1
0 1569 538
1 0 408
is a little confusing because it's not immediately obvious which is which. Of course, I checked out ?table but I don't see an option to do this, at least not a logical switch that doesn't require me to already know which is which.
I tried ftable but had the same problem.
The output I want would be something like this:
> table(tc$tr, tc$fall_term)
tr tr
0 1
fallterm 0 1569 538
fallterm 1 0 408
or
> table(tc$tr, tc$fall_term)
fallterm fallterm
0 1
tr 0 1569 538
tr 1 0 408

You can use the dnn option :
table(df$tr,df$fall_term) # impossible to tell the difference
0 1
0 18 33
1 15 34
table(df$tr,df$fall_term,dnn=c('tr','fall_term')) # you have the names
fall_term
tr 0 1
0 18 33
1 15 34
Note that it's easier (and safer) to do table(df$tr,df$fall_term,dnn=colnames(df))

Check out dimnames, and in particular their names. I’m using another example here since I don’t have your data:
x = HairEyeColor[, , Sex = 'Male']
names(dimnames(x))
# [1] "Hair" "Eye"
names(dimnames(x)) = c('Something', 'Else')
x
# Else
# Something Brown Blue Hazel Green
# Black 32 11 10 3
# Brown 53 50 25 15
# Red 10 10 7 7
# Blond 3 30 5 8

Related

Creating Tables and Bar Plots in R as a Beginner

I've begun using R recently so this might be simple to solve. I actually have two problems but I believe they`re connected.
I have a simple dataset (.csv file with 3 columns and 7 rows) and I'm trying to create a table out of it and plot a bar graph with the values of the two numerical columns.
Grupo de idade;Freq. Relativa Homens;Freq. Relativa Mulheres
16 a 19;0,411;0,415
20 a 24;0,787;0,701
25 a 34;0,922;0,745
35 a 44;0,923;0,755
45 a 54;0,882;0,760
55 a 64;0,696;0,583
65 ou mais;0,205;0,126
df = read.csv(filename, header = TRUE, sep = ";")
tab = table(df)
sd = cbind(df$Freq.Homens, df$Freq.Mulheres)
barplot(sd, beside = TRUE)
So first my table ends up looking like this, with the values as headers:
Freq..Relativa.Homens
Grupo.de.idade 0,205 0,411 0,696 0,787 0,882 0,922 0,923
16 a 19 0 0 0 0 0 0 0
20 a 24 0 0 0 0 0 0 0
25 a 34 0 0 0 0 0 0 0
35 a 44 0 0 0 0 0 0 0
45 a 54 0 0 0 0 1 0 0
55 a 64 0 0 0 0 0 0 0
65 ou mais 0 0 0 0 0 0 0
And my graph is plotted with integers values like 2, 4, and 6. I noticed that happened because of the cbind function, but without it, I can`t plot anything.
First: R thinks anglo-american (; , i.e. the decimal mark is a ".".
The decimal mark in your data is a ",". You have to tell this to R, by adding the argument `dec = ","``, i.e.
df = read.csv(filename, header = TRUE, sep = ";". dec = ",")
Otherwise R interprets the numbers as characters or strings
table makes a contigency table of two variables. This however makes only sense for categorical variables, e.g. number of observations by age and sex.
You have only one categorical variable (Grupo.de.idade) and two continuous variables
R does the best to make sense of this, and simply interprets the values of the continuous variables as categories, which however makes no sense, e.g there is 1 observation in your data set with "Grupo de idade" = "16 a 19" and a value of "0,411" for "Freq. Relativa Homens". That's what table is telling you.
Moreover your data is already in table format so if you want to have a look at your data simply type df to the console
df
#> Grupo.de.idade Freq..Relativa.Homens Freq..Relativa.Mulheres
#> 1 16 a 19 0.411 0.415
#> 2 20 a 24 0.787 0.701
#> 3 25 a 34 0.922 0.745
#> 4 35 a 44 0.923 0.755
#> 5 45 a 54 0.882 0.760
#> 6 55 a 64 0.696 0.583
#> 7 65 ou mais 0.205 0.126
The easiest way to meke a simple barplot is like this:
barplot(Freq..Relativa.Homens ~ Grupo.de.idade, data = df)
On the left of the "~" put the variable to plot, on the right the grouping variable. Furthermore you have to tell R the name of the dataset.
However, instead of a trial-and-error-approach to R I recommend to work through the introductory chapters of one of the free tutorials or textbooks one can find on the internet, like The Pirate's guide to R
Created on 2020-03-27 by the reprex package (v0.3.0)

Mapping dataframe column values to a n by n matrix

I'm trying to map column values of a data.frame object (consisting of large number of bilateral trade data among 161 countries) to a 161 x 161 adjacency matrix (also of data.frame class) such that each cell represents the dyadic trade flows between any two countries.
The data looks like this
# load the data from dropbox folder
library(foreign)
example_data <- read.csv("https://www.dropbox.com/s/hf0ga22tdjlvdvr/example_data.csv?dl=1")
head(example_data, n = 10)
rid pid TradeValue
1 2 3 500
2 2 7 2328
3 2 8 2233465
4 2 9 81470
5 2 12 572893
6 2 17 488374
7 2 19 3314932
8 2 23 20323
9 2 25 10
10 2 29 9026220
length(unique(example_data$rid))
[1] 139
length(unique(example_data$pid))
[1] 161
where rid is reporter id, pid is (trade) partner id, a country's rid and pid are the same. The same id(s) in the rid column are matched with multiple rows in the pid column in terms of TradeValue.
However, there are some problems with this data. First, because countries (usually developing countries) that did not report trade statistics have no data to be extracted, their id(s) are absent in the rid column (such as country 1). On the other hand, those country id(s) may enter into pid column through other countries' reporting (in which case, the reporters tend to be developed countries). Hence, the rid column only contains some of the country id (only 139 out of 161), while the pid column has all 161 country id.
What I'm attempting to do is to map this example_data dataframe to a 161 x 161 adjacency matrix using rid for row and pid for column where each cell represent the TradeValue between any two country id. To this end, there are a couple things I need to tackle with:
Fill in those country id(s) that are missing in the rid column of example_data and, temporarily, set all cell values in their respective rows to 0.
By previous step, impute those "0" cells using bilateral trade statistics reported by other countries; if the corresponding statistics are still unavailable, leave those "0" cells as they are.
For example, for a 5-country dataframe of the following form
rid pid TradeValue
2 1 50
2 3 45
2 4 7
2 5 18
3 1 24
3 2 45
3 4 88
3 5 12
5 1 27
5 2 18
5 3 12
5 4 92
The desired output should look like this
pid_1 pid_2 pid_3 pid_4 pid_5
rid_1 0 50 24 0 27
rid_2 50 0 45 7 18
rid_3 24 45 0 88 12
rid_4 0 7 88 0 92
rid_5 27 18 12 92 0
but on top of my mind, I could not figure out how to. It will be really appreciated if someone can help me on this.
df1$rid = factor(df1$rid, levels = 1:5, labels = paste("rid",1:5,sep ="_"))
df1$pid = factor(df1$pid, levels = 1:5, labels = paste("pid",1:5,sep ="_"))
data.table::dcast(df1, rid ~ pid, fill = 0, drop = FALSE, value.var = "TradeValue")
# rid pid_1 pid_2 pid_3 pid_4 pid_5
#1 rid_1 0 0 0 0 0
#2 rid_2 50 0 45 7 18
#3 rid_3 24 45 0 88 12
#4 rid_4 0 0 0 0 0
#5 rid_5 27 18 12 92 0
The secrets/ tricks:
use factor variables to tell R what values are all possible as well as the order.
in data.tables dcast use fill = 0 (fill zero where you have nothing), drop = FALSE (make entries for factor levels that aren't observed)

Feature selection with "fscaret" in R issue with empty list()

I have a data frame that has a lot of factor and numeric variables and I'm trying to use fscaret package for feature selection.
Sample of the data:
age clerical construc educ earns74 gdhlth inlf leis1 rlxal
32 0 0 12 0 0 1 3529 3479
40 1 0 14 9500 1 1 3929 3329
20 1 1 10 329 0 0 5300 2309
22 0 0 6 602 1 0 5205 4290
I try to run the following code:
fsMod <- c("gbm", "treebag", "ridge", "lasso", "Boruta", "glm")
myFS<-fscaret(train.sleepDF, test.sleepDF, myTimeLimit = 40, preprocessData=TRUE, Used.funcRegPred = 'fsMod', with.labels=TRUE,
supress.output=FALSE, no.cores=2)
And then finding the myFS$VarImp I get list()
I've read the annotation to the package and they also report on that issue, not giving a clear solution to it. There appears to be a troublesome method in the calculations, however, how do I identificate it?
Is there a way to solve the problem?
Any help is greatly appreciated.

Optimization of an R loop taking 18 hours to run

I've got an R code that works and does what I want but It takes a huge time to run. Here is an explanation of what the code does and the code itself.
I've got a vector of 200000 line containing street adresses (String) : data.
Example :
> data[150000,]
address
"15 rue andre lalande residence marguerite yourcenar 91000 evry france"
And I have a matrix of 131x2 string elements which are 5grams (part of word) and the ids of the bags of NGrams (example of a 5Grams bag : ["stack", "tacko", "ackov", "ckover", ",overf", ... ] ) : list_ngrams
Example of list_ngrams :
idSac ngram
1 4 stree
2 4 tree_
3 4 _stre
4 4 treet
5 5 avenu
6 5 _aven
7 5 venue
8 5 enue_
I have also a 200000x31 numerical matrix initialized with 0 : idv_x_bags
In total I have 131 5-grams and 31 bags of 5-grams.
I want to loop the string addresses and check whether it contains one of the n-grams in my list or not. If it does, I put one in the corresponding column which represents the id of the bag that contains the 5-gram.
Example :
In this address : "15 rue andre lalande residence marguerite yourcenar 91000 evry france". The word "residence" exists in the bag ["resid","eside","dence",...] which the id is 5. So I'm gonna put 1 in the column called 5. Therefore the corresponding line "idv_x_bags" matrix will look like the following :
> idv_x_sacs[150000,]
4 5 6 8 10 12 13 15 17 18 22 26 29 34 35 36 42 43 45 46 47 48 52 55 81 82 108 114 119 122 123
0 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
Here is the code that does :
idv_x_sacs <- matrix(rep(0,nrow(data)*31),nrow=nrow(data),ncol=31)
colnames(idv_x_sacs) <- as.vector(sqldf("select distinct idSac from list_ngrams order by idSac"))$idSac
for(i in 1:nrow(idv_x_bags))
{
for(ngram in list_ngrams$ngram)
{
if(grepl(ngram,data[i,])==TRUE)
{
idSac <- sqldf(sprintf("select idSac from list_ngramswhere ngram='%s'",ngram))[[1]]
idv_x_bags[i,as.character(idSac)] <- 1
}
}
}
The code does perfectly what I aim to do, but it takes about 18 hours which is huge. I tried to recode it with c++ using Rcpp library but I encountered many problems. I'm tried to recode it using apply, but I couldn't do it.
Here is what I did :
apply(cbind(data,1:nrow(data),1,function(x){
apply(list_ngrams,1,function(y){
if(grepl(y[2],x[1])==TRUE){idv_x_bags[x[2],str_trim(as.character(y[1]))]<-1}
})
})
I need some help with coding my loop using apply or some other method that run faster that the current one. Thank you very much.
Check this one and run the simple example step by step to see how it works.
My N-Grams don't make much sense, but it will work with actual N_Grams as well.
library(dplyr)
library(reshape2)
# your example dataset
dt_sen = data.frame(sen = c("this is a good thing", "this is bad"), stringsAsFactors = F)
dt_ngr = data.frame(id_ngr = c(2,2,2,3,3,3),
ngr = c("th","go","tt","drf","ytu","bad"), stringsAsFactors = F)
# sentence dataset
dt_sen
sen
1 this is a good thing
2 this is bad
#ngrams dataset
dt_ngr
id_ngr ngr
1 2 th
2 2 go
3 2 tt
4 3 drf
5 3 ytu
6 3 bad
# create table of matches
expand.grid(unique(dt_sen$sen), unique(dt_ngr$id_ngr)) %>%
data.frame() %>%
rename(sen = Var1,
id_ngr = Var2) %>%
left_join(dt_ngr, by = "id_ngr") %>%
group_by(sen, id_ngr,ngr) %>%
do(data.frame(match = grepl(.$ngr,.$sen))) %>%
group_by(sen,id_ngr) %>%
summarise(sum_success = sum(match)) %>%
mutate(match = ifelse(sum_success > 0,1,0)) -> dt_full
dt_full
Source: local data frame [4 x 4]
Groups: sen
sen id_ngr sum_success match
1 this is a good thing 2 2 1
2 this is a good thing 3 0 0
3 this is bad 2 1 1
4 this is bad 3 1 1
# reshape table
dt_full %>% dcast(., sen~id_ngr, value.var = "match")
sen 2 3
1 this is a good thing 1 0
2 this is bad 1 1

expected number in from data in data.frame in R

I want to turn this equation into an R code: ((e^-mean)(mean^i)/i!)XN; where i = index and N is sample size.
What I have is this:
x["expected92"]<-((exp(-me92))(me92^(x$multX1992))/(x$multX1992));
I want to create a new column that goes through the index and makes the expected mean.
example data:
Drag 1992 multX1992
0 113 0
1 30 30
3 15 30
example of wanted output:
Drag 1992 multX1992 expected92
0 113 0 90.03
1 30 30 58.80
3 15 30 19.20
Can someone help fix my code?

Resources