简体   繁体   中英

Grab two variables from structured character vector and create data frame

Let's have a following vector:

vector <- c("0:00 0,6 0:00", "5:00 1,2 5:00","9:30 0,9 22:00","16:00 1,0","21:30 0,9")

We see that element contains:

hours,number (for instance "0,6"), hour2 (or blank)

It seems structured: after ":" are always two digits ("00" or "30") then "" and number with decimal point (comma).

I want to create data frame and get data frame containing first hour and given number, like:

#Expected result:
df
$hours $value
#0:00   0.6
#5:00   1.2
#9:30   0.9
#16:00  1.0
#21:30  0.9

You can try:

data.frame(hours = sapply(strsplit(vector, " "), function(x) x[1]),
value = sapply(strsplit(vector, " "), function(x) x[2]))

  hours value
1  0:00   0,6
2  5:00   1,2
3  9:30   0,9
4 16:00   1,0
5 21:30   0,9

It , first, splits the vector by strsplit() , then combines the first and second element in a data.frame .

If you also want to replace the comma with a decimal:

data.frame(hours = sapply(strsplit(vector, " "), function(x) x[1]),
value = sub(",", ".", sapply(strsplit(vector, " "), function(x) x[2])))

  hours value
1  0:00   0.6
2  5:00   1.2
3  9:30   0.9
4 16:00   1.0
5 21:30   0.9

It does the same as the code above, but it is also replacing comma in the second element by decimal using sub() .

Or:

df <- read.table(text = vector, sep = " ", dec = ",", as.is = TRUE, fill = TRUE)[, 1:2]
colnames(df) <- c("hours", "value")

  hours value
1  0:00   0.6
2  5:00   1.2
3  9:30   0.9
4 16:00   1.0
5 21:30   0.9

It converts the vector to a data.frame , with blank space used as separator and comma used as decimal, and then selects the first two columns.

Try:

vec1<-sapply(strsplit(vector," "),"[")
df<-plyr::ldply(vec1,function(x) x[1:2])
names(df)<-c("hours","value")       
df$value<-gsub(",",".",df$value)

Result:

  hours value
1  0:00   0.6
2  5:00   1.2
3  9:30   0.9
4 16:00   1.0
5 21:30   0.9

Another fun solution is to use word from stringr package, ie

library(stringr)
data.frame(hours = word(vector, 1), 
           values = as.numeric(sub(',', '.', word(vector, 2), fixed = TRUE)), 
           stringsAsFactors = FALSE)

which gives,

  hours values 1 0:00 0.6 2 5:00 1.2 3 9:30 0.9 4 16:00 1.0 5 21:30 0.9 

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