0

I have a problem with my data. I want to keep my data in one file. There are few csv files which I loaded into R.

 > list_of_data
 [1] "Weight.csv" "Lenght.csv" "Age.csv" "Place of Birth.csv"           
 [5] "Sex.csv" "Driver License.csv" "Nationality.csv" 

In each of the file I have a single information about the patient:

Weight.csv:

Name    Weight         
Mark      76
Criss     82
Kate      61
Robb      80
Denis     72

Age.csv:

Name      Age         
Mark      19
Criss     17
Kate      24
Robb      33
Denis     23

I want to move all of the informations to one single matrix. So I created a new matrix:

data_mat <- matrix(0, nrow = 5,ncol = 7)
colnames(data_mat) <- c("Name", "Weight", "Lenght", "Age", "Place of Birth", "Sex", "Driver License", "Nationality")
rownames(data_mat) <- c("Mark", "Criss", "Kate", "Robb", "Denis")

I don't know how to tell R to find the information about the patient in other csv files and put them together in my new matrix. Any ideas ?

Juanhijuan
  • 102
  • 1
  • 3
  • 9

2 Answers2

1

You're going to want to merge all of your individual csv's together.

dat.all<-merge(csv1,csv2,by="name")
dat.all<-merge(dat.all,csv3,by="name")
...

just do this across all your csv files and then you'll have what you're looking for

Steve Reno
  • 1,304
  • 3
  • 14
  • 21
1

You can also use cbind across all the files, removing the name column of all but the first.

Weight <- data.frame(Name = c("Mark", "Criss", "Kate"), Weight = c(76, 82, 61))
Age <- data.frame(Name = c("Mark", "Criss", "Kate"), Age = c(19, 17, 24))

cbind(Weight, Age = Age[, "Age"])
##    Name Weight Age
## 1  Mark     76  19
## 2 Criss     82  17
## 3  Kate     61  24
Thomas
  • 43,637
  • 12
  • 109
  • 140
Rich Scriven
  • 97,041
  • 11
  • 181
  • 245
  • @Thomas, Should we always comment out the result? I didn't know that. – Rich Scriven Feb 27 '14 at 14:42
  • Not necessarily. I usually typeset larger results separately from the code, but if it's just a few lines like here, it's easy to just put the result in comments to that it kind of mimics the console output. – Thomas Feb 27 '14 at 15:08