FishareR FishareR - 1 month ago 9
R Question

Inserting data into a vector after relating it to a dataframe

I am working with a data frame called "taxa1", imported via csv from Access that has site data for different areas, under the col name "site.c" that I converted into a character vector.

I wish to create variable names that are the same as the site names by creating a vector that holds all of the site names, which I have already done. However, I am stuck on matching the vector names to the data in the col, and then pulling out the data when it matches the vector value. Here is my current code.

site <- unique(taxa1$site.c)

for (i in site) {
assign(paste("",i,sep =""), i )
if (which(taxa1$site.c %in% "")) {
subset(taxa1,site.c)
}
stop
}


Can anyone help me finish up the if part?

Thank you.

Answer

Subset should only be used in interactive mode, not in a program nor in a script. You should use [ instead.
If I understood correctly you want to separate into new variables the data on your taxa1 object according to a variable:

for (area in unique(taxa1$site.c)) {
    assign(area, # Assign to the create variable to that area
           taxa1[taxa1$site.c == area, # Select just the data of that area
                 !"site.c" %in% colnames(taxa1)]) # remove the column of the area
}