[R] R Beginner : Loop and adding row to dataframe

arun smartpink111 at yahoo.com
Sun Jul 22 22:42:53 CEST 2012


Hi, 


Try this:

dat1<-read.table(text="
NACE aaa bbb ccc
1 a a c
1 a a c
1 a a c
2 a a c
2 a a c
3 a a c
4 a a c
4 a a c
4 a a c
",sep="",header=TRUE)
dat2<-read.table(text="
Name NACE
a 1
b 2
c 3
",sep="",header=TRUE)

 dat3<-merge(dat1,dat2)
 dat3<-dat3[,1:4]
 dat3
  NACE aaa bbb ccc
1    1   a   a   c
2    1   a   a   c
3    1   a   a   c
4    2   a   a   c
5    2   a   a   c
6    3   a   a   c
A.K.






----- Original Message -----
From: ph!l <firestorm1 at hotmail.com>
To: r-help at r-project.org
Cc: 
Sent: Sunday, July 22, 2012 12:35 PM
Subject: [R] R Beginner : Loop and adding row to dataframe

Hi everybody,

I am currently quite inexperienced with R.
I try to create a function that simply take a value in a dataframe, look for
this value in another dataframe and copy all the row that have this value
This example is a simplified version of what I am doing but it's enough to
help me

listA
Name NACE
a 1
b 2
c 3

ListB
NACE aaa bbb ccc
1 a a c
1 a a c
1 a a c
2 a a c
2 a a c
3 a a c
4 a a c
4 a a c
4 a a c

The output i would like to have
NACE aaa bbb ccc
1 a a c
1 a a c
1 a a c
2 a a c
2 a a c
3 a a c

Code: 

listpeer <- function (x) {
  for (i in 1:length(listA$NACE)) 
    TriNACE[i] <- subset.data.frame(ListB, NACE == NACEsample$NACE[i],)
    TriNACE
}

But the result is
Warning message:
In `[<-.data.frame`(`*tmp*`, i, value = list(NACE = c(3L, 3L, 3L :
provided xx variables to replace x variables"
I guess there is something wrong "TriNACE[i]", instead i should use
something to add rows, but I really don't find anything ?
Somebody has any clue ?

Thank you for your time and help! 



--
View this message in context: http://r.789695.n4.nabble.com/R-Beginner-Loop-and-adding-row-to-dataframe-tp4637360.html
Sent from the R help mailing list archive at Nabble.com.

______________________________________________
R-help at r-project.org mailing list
https://stat.ethz.ch/mailman/listinfo/r-help
PLEASE do read the posting guide http://www.R-project.org/posting-guide.html
and provide commented, minimal, self-contained, reproducible code.




More information about the R-help mailing list