Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/r/79.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Sql 如何从CSV输出中删除换行符_Sql_R - Fatal编程技术网

Sql 如何从CSV输出中删除换行符

Sql 如何从CSV输出中删除换行符,sql,r,Sql,R,我正在尝试在csv文件中添加一个新列,以便在“说明”列包含文本字符串时在新列中添加“1”。看起来代码正常工作,但是write.csv在“Description”列中添加了换行符,从而将输出文件弄乱了 我是R新手,已经尝试了一些东西,包括使用 图书馆(stringr) 但是它被卡住了 data = read_excel("database.xlsx") data1 = sqldf(c("alter table data add newcolumn numeric","select * from d

我正在尝试在csv文件中添加一个新列,以便在“说明”列包含文本字符串时在新列中添加“1”。看起来代码正常工作,但是write.csv在“Description”列中添加了换行符,从而将输出文件弄乱了

我是R新手,已经尝试了一些东西,包括使用 图书馆(stringr)

但是它被卡住了

data = read_excel("database.xlsx")
data1 = sqldf(c("alter table data add newcolumn numeric","select * from data"))
data1 = sqldf(c("update data1 set newcolumn = case when Description LIKE '%pyramidlike%' then 1 else 0
        end", "select * from data1"))
write.csv(data1, "data1.csv")

您没有包含样本数据,使其成为可复制的示例。但是,有更简单的方法来实现您想要做的事情,我将使用一个示例数据集(
iris
):

然后将数据帧写入CSV:

# I am using the tidyverse version of the function
write_csv(
  new_iris,
  path = "/path/to/output.csv"
)
对于这种特殊情况,使用
sqldf()
操作de数据集没有任何好处。通常,当我直接在数据库中进行更改时,我会使用它


“它卡住了”是什么意思?意思是没有输出。我只看到红色的停止图标,没有结果谢谢。在您的数据中,如何筛选“包含”文本字符串“sentosa”的所有行。我不是在寻找==我的意思是,当列“包含”文本字符串“setosa”时进行mutate(在SQL中,我会像“%setosa%”一样编写检查
str_detect()
function()或一个基本的R regex函数(请参阅:)
library(tidyverse)

# will use the IRIS dataset instead of your XLSX, adjust as needed.
data(iris)
new_iris <- iris %>% 
  mutate(
    flag = ifelse(Species == "setosa", 1, 0)  # add a new column
  )
> str(iris)
'data.frame':   150 obs. of  5 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...

> str(new_iris)
'data.frame':   150 obs. of  6 variables:
 $ Sepal.Length: num  5.1 4.9 4.7 4.6 5 5.4 4.6 5 4.4 4.9 ...
 $ Sepal.Width : num  3.5 3 3.2 3.1 3.6 3.9 3.4 3.4 2.9 3.1 ...
 $ Petal.Length: num  1.4 1.4 1.3 1.5 1.4 1.7 1.4 1.5 1.4 1.5 ...
 $ Petal.Width : num  0.2 0.2 0.2 0.2 0.2 0.4 0.3 0.2 0.2 0.1 ...
 $ Species     : Factor w/ 3 levels "setosa","versicolor",..: 1 1 1 1 1 1 1 1 1 1 ...
 $ flag        : num  1 1 1 1 1 1 1 1 1 1 ...
# I am using the tidyverse version of the function
write_csv(
  new_iris,
  path = "/path/to/output.csv"
)