[英]combine multiple columns read.csv in r
IS there a way to combine multiple columns into a single column while reading data from csv files. 是一种从csv文件读取数据时将多个列合并为一个列的方法。
Example 例
My data in file is in below format ID,DOB,FirstName,LastName,DOJ 我文件中的数据采用以下格式ID,DOB,FirstName,LastName,DOJ
In read.csv/read.table is it possible to convert this into a four column, where First and Last names are combined to get a new column called Name. 在read.csv / read.table中,可以将其转换为四列,其中“姓氏”和“姓氏”组合在一起以获得名为Name的新列。
I would do it with the sqldf
package eg: 我会用sqldf
包做到这一点,例如:
require(sqldf)
# Example Data
df <- data.frame(
ID = 1:5,
DOB = 1:5,
FirstName = c("a", "b", "c", "d", "e"),
LastName = c("A", "B", "C", "D", "E"),
DOJ = 1:5)
# Write to disc to load it afterwards
write.csv(df, "example.csv", quote = FALSE, row.names = FALSE)
# Using SQL to combine ...
sql <- "Select ID, DOB, FirstName || ' ' || LastName as Name from file"
out <- read.csv.sql("example.csv", sql = sql)
out
# ID DOB Name
# 1 1 1 a A
# 2 2 2 b B
# 3 3 3 c C
# 4 4 4 d D
# 5 5 5 e E
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.