1

Apologies for any poorly formed question - I'm very new to R.

I am looking to create multiple character strings from this data table..

I have created the character string: coor1 <- R_data[1,8]

I am looking to iterate this for other indices as follows:

coor1 <- data[1,8]
coor2 <- data[2,8]
coor3 <- data[3,8]
coor4 <- data[4,8]
coor5 <- data[5,8] etc.... 

I have tried using a for loop but with no success. Any advice would be great.

Thanks very much.

  • Your question isn't clear. Please provide a [mcve]. See [How to make a great R reproducible example?](https://stackoverflow.com/q/5963269/4996248) for what this would mean in R. – John Coleman May 21 '20 at 12:50

1 Answers1

0

I think you just need to subset the 8th column with $:

data <- data.frame(V1 = rep(NA, 26))
data[, 2:7] <- NA
data[, 8] <- letters

data$V8
[1] "a" "b" "c" "d" "e" "f" "g" "h" "i" "j" "k" "l" "m" "n" "o" "p" "q" "r" "s" "t" "u"
[22] "v" "w" "x" "y" "z"

data[1, 8]
[1] "a"

data[2, 8]
[1] "b"

data[3, 8]
[1] "c"

data[4, 8]
[1] "d"

You can assign it to a variable as well:

coor <- data$V8

And extract a single result with []:

coor[1]
[1]"a"

coor[2]
[1] "b"

This can also be accomplished from the original dataframe:

data$V8[1]
[1] "a"

which is same as:

data[1, 8]
[1] "a"

What a loop would look like:

coors <- vector() #allocate space for storage
for(i in seq_len(nrow(data))){
  coors[i] <- data[i, 8]
}
bs93
  • 1,236
  • 6
  • 10