简体   繁体   中英

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

First convert your columns to characters

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

And then rerun the code

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM