I have 3 DF:
- "CAR" with 100 rows and 2 columns
- "BIKE" with 60 rows and 3 columns
- "ELENCO_DF" with 2 rows and 1 column
ELENCO_DF is like that
Nome CAR BIKE
Each element rappresent the "name" of the other DF
#for each element of ELENCO_DF I would like to print the following
for (i in 1:nrow(ELENCO_DF)){
print(nrow(ELENCO_DF[i]))
}
#Thanks a lot in advance
CodePudding user response:
Please try this:
for (i in 1:nrow(ELENCO_DF)){
print(nrow(get(ELENCO_DF[i,])))
}
With the "get" function, R will know that the string "CAR" or "BIKE" are actually dataframes and will know what they "contain".
You may read more about it by writing the command
?get
in R.
CodePudding user response:
Assuming your dataframe ELENCO_DF is defined by
ELENCO_DF <- data.frame(Nome = c("CAR","BIKE"))
Try this (adding a comma after the "i")
for (i in 1:nrow(ELENCO_DF)){
print(nrow(ELENCO_DF[i,]))
}
Because ELENCO_DF is a dataframe, with two types of dimensions: rows and columns. So when you need to call an element from a dataframe you have to inform which rows and columns you want. In this case you want one row at the time (the "i") and all the columns (the empty space after the comma). If you wanted row i and column j, you would need to call ELENCO_DF[i,j]. Even if there is only one column.
Hope it helped!