2
votes

I'm trying to use the logisticPCA package with my own data. The functions of this package only work with binary data which is what i'm looking for but, the first column which usually contains the observation name or group is taken as a regular variable and doesn't work since it's non-binary data.

The package itself contains a data named house_votes84 which, although having the first column as the group name of each observation (democrat/republican) it's not recognized as a variable and the functions of the package work perfectly. Actually, in this website: some graphs are created by first extracting the row names of the first column:

party = rownames(house_votes84)

I have tried many different ways to import a data.frame (mainly .csv from the notepad, with the first row, the headers, with one less name) with the first column containing the names but not being considered as a variable without success.

How do I creater or emulate this data structure in R?

               handicapped-infants water-project-cost-sharing
republican                   0                          1
republican                   0                          1
democrat                    NA                          1
democrat                     0                          1
democrat                     1                          1

Data.frame from house_votes84 simplified (row.names and 2 variables instead of 16)

2
Are you reading your data with read.csv or similar? If so, give a glance in ?read.table to the row.names argument. Otherwise, say that the column containing the row names is the first and df is your data.frame. You can set the row names with rownames(df)<-df[[1]] and then eliminate the column with df[[1]]<-NULL.nicola
Hi. I think i have tried this before but I still gave it a shot. read.table tells me that either i'm duplicating row.names when 1 is given or simply names the first column "row.names" when NULL is given. The other way you proposed also prompts me a message saying i'm duplicating row.names. Thanks anyway!N.S.I

2 Answers

0
votes
#Load Data
library(logisticPCA)
data("house_votes84")

#Data is a list of two components:
str(house_votes84)

#Create an object with only the first two columns:
x <- house_votes84[,1:2]

#Or making it a data.frame:
df  <- as.data.frame(house_votes84[,1:2])

#Add column for party
df$party <- rownames(df)
0
votes

Create a file like text or csv file:

Party, handicapped-infants, water-project-cost-sharing
republican                   0                          1
republican                   0                          1
democrat                    NA                          1
democrat                     0                          1
democrat                     1                          1

Then load it:

df2 <- read.table("house.txt", header= T)
df3 <-read.csv("house.csv", header=T)