简体   繁体   中英

Splitting a column in a dataframe in R into two based on content

I have a column in a R dataframe that holds a product weight ie 20 kg but it has mixed measuring systems ie 1 lbs & 2 kg etc. I want to separate the value from the measurement and put them in separate columns then convert them in a new column to a standard weight. Any thoughts on how I might achieve that? Thanks in advance.

Assume you have the column given as

x <- c("20 kg","50 lbs","1.5 kg","0.02 lbs")

and you know that there is always a space between the number and the measurement. Then you can split this up at the space-character, eg via

splitted <- strsplit(x," ")

This results in a list of vectors of length two, where the first is the number and the second is the measurement. Now grab the numbers and convert them via

numbers <- as.numeric(sapply(splitted,"[[",1))

and grab the units via

units <- sapply(splitted,"[[",2)

Now you can put everything together in a `data.frame.

Note: When using as.numeric , the decimal point has to be a dot. If you have commas instead, you need to replace them by a dot, for example via gsub(",","\\\\.",...) .

separate(DataFrame, VariableName, into = c("Value", "Metric"), sep = " ")

My case was simple enough that I could get away with just one space separator but I learned you can also use a regular expression here for more complex separator considerations.

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