This achieves a data.frame with numeric Sample
column and character Type
column, as your example suggests. As others have mentioned, there are many ways to accomplish this.
sample <- c('1000 RV',
'456 LV',
'678 A')
A <- strsplit(sample, '\\s+') # Split by whitespace. Returns a list
B <- unlist(A) # Converts 1:3 list to a 6x1 character vector
C <- matrix(B, ncol = 2, byrow = T) # Convert 6x1 character vector to 3x2 matrix
D <- as.data.frame(C, stringsAsFactors = F) # Convert matrix to data.frame so columns can be different types
# All together...
D <- as.data.frame(matrix(unlist(strsplit(sample, '\\s+')), ncol = 2, byrow = T),
stringsAsFactors = F)
D[ ,1] <- as.numeric(D[ ,1]) # Convert first column to numeric, second remains character
colnames(D) <- c('Sample', 'Type') # Add column names
> D
Sample Type
1 1000 RV
2 456 LV
3 678 A
> str(D)
'data.frame': 3 obs. of 2 variables:
$ Sample: num 1000 456 678
$ Type : chr "RV" "LV" "A"