Reputation: 137
I am trying to read in a table in R then check if the category column of the table matches a certain value, if it does copy the value of a different column over. Thus creating an array of values who's category tag matches with the conditional
B <- read.table("tableFile2.data", sep=";", header=TRUE, row.names=2)
B$new <- ifelse(B$category == "mammal", B$animal, NA)
B$new
With the file
wasted space
more waste
animal;RowName;category
dog;a;mammal
spider;b;insect
monkey;c;mammal
sardine;d;fish
Jeromy;J;angry
However when I execute this and print B$new I get the values
1 NA 2 NA
So the program clearly works for the condition but the correct value is not being copied over
Upvotes: 0
Views: 39
Reputation: 3427
First of all, the first query (if the file actually looks like the one that you have described in the question) should be:
B <- read.table("tableFile2.data", sep=";", header=TRUE, skip=2)
For accomplishing the above mentioned task, you can run this query:
B$new <- ifelse(B$category == "mammal", as.character(B$animal), NA)
The reason behind the output that you mentioned is the fact that in this data frame B, the column 'animal' is taken as a FACTOR and not character.
The numbers that you are getting in your output are similar to one obtained if you cast B$animal as 'integer'.
Upvotes: 1