Rawshn
Rawshn

Reputation: 37

How to append strings to a vector in R?

I am new to R. I am trying to append values from a data frame like this here is the data frame tu :

         t     u
1     What   LOL
2 Whatever   ALL
3    Works   OLO
4     What  POLO
5 Whatever CHOLO
6 Whatever  LOLO
7    Works     C
8 Whatever     D

I want to print the values of u for which t is "Whatever"

a <- vector()
for(i in 1:8) {
if(tu$t[i] == 'Whatever') {
  a<-c(a,tu$u[i])
}}

When the execution is complete I am getting the value of print(a) as an integer type Vector instead of a vector of a set of strings. "ALL CHOLO LOLO D"

The output is int [1:4] 1 3 6 4 Can anybody explain what is happening? PS: Ignore the values of u :P

Upvotes: 0

Views: 1521

Answers (1)

iskandarblue
iskandarblue

Reputation: 7526

First convert your columns to characters

tu$t <- as.character(tu$t)
tu$u <- as.character(tu$u)

And then rerun the code

Upvotes: 1

Related Questions