R appends country names as numbers in vector

137 views Asked by At

I am pretty new to R and want to work with it regularly from now on. Therefore, many questions arise, of which the most can be solved by some research.

In this special case, however, I cannot find the problem.

I want to create a panel data set and have to reshape data therefor. I need to reshape according to certain criteria. To do so, I have written some for-loops, which are working pretty good.

Only in one case, the loop (or more concrete the append function) does not work as I want it to.

My goal is to create a vector of country names, in which each country (200 at all) is repeated 48 times, since I have data for 48 years. The country names are given and I have created a vector, with all of them

country_vec <- unique(Data, [ , 1])

Second I have created an empty vector, to fill in the 48 repetitions

countryname_panel <- c()

Third, I want to write the for-loop, to perform the repetition and the fill into the empty vector, for cbinding it later on, with the other created vectors.

for (i in 1:200){
x = rep(country_vec[i], 48)
countryname_panel = append(countryname_panel, x)
}

The values for "x" are what I need and when I print them, I can see the countries 48 times. As soon as the step with "append" is performed however, and I print "countryname_panel", the countries are shown as numbers ("1" 48 times, "2" 48 times, and so on), not as country names anymore.

Does anyone have an idea what is not working correctly here and where I have made a mistake?

I am looking forward to your answers and help.

Thanks in advance

0

There are 0 answers