0

Here is my data:

require(HH)
data(ProfChal)
rowsCount = length(ProfChal$Question)
ProfChal$NEW = character(rowsCount)

It looks like this:
HH ProfChal dataset

When I run this loop:

for (r in 1:rowsCount){
  ProfChal[r,"NEW"] = ProfChal[r,"Subtable"]
}

the new column has integers in it:
Results table after running for loop - integers filled in

I would like to have text values instead of integers. Debugging it leaves me confused...

ProfChal[2,"Subtable"] returns [1] Employment sector.
ProfChal[1,"NEW"] = "asdf" works as expected.

3
  • I know about df$TargetCol <- df$SourceCol, but originally, I don't want it. I created this example loop just for simplicity. Commented Apr 10, 2019 at 7:10
  • And yet, accepted answer is doing exactly what you are doing in your comment, but with as.character around it. Commented Apr 10, 2019 at 8:24
  • @zx8754 - Because of the answer, I have applied as.character on elements in my loop, not on full column, which was not possible using aforementioned syntax. Commented Apr 10, 2019 at 9:41

1 Answer 1

2

The problem is that the column Subtable is stored as a factor not as character. You can check this by typing class(ProfChal[ ,"Subtable"]).

You can convert this column to character using as.character:

ProfChal[, "NEW"] = as.character(ProfChal[, "Subtable"])

Note also, that you do not need a for loop in this example.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.