【问题标题】:How to write a single text file from two different data frame-one has name and second has values using R如何从两个不同的数据框写入单个文本文件 - 一个具有名称,第二个具有使用 R 的值
【发布时间】:2017-05-19 19:07:23
【问题描述】:

我有两个数据框。我想创建一个文本文件。它是水文模型的输入文件 感谢您的帮助。我试图编写文件代码格式,但它不接受。它也是一个等号的单列。我认为给定所需的输出文件解释得很好。

第一个数据框为

> lfz
   readLines("G:/Rlearning/wrds.txt")
1                    HYDRUS_Version =
2                         WaterFlow =
3                   SoluteTransport =
4                         Unsatchem =
5                         Unsatchem =
6                               HP1 =
7                     HeatTransport =
8             EquilibriumAdsorption =
9                    MobileImmobile =
10                  RootWaterUptake =
11                       RootGrowth =
12                  MaterialNumbers =
13                 SubregionNumbers =
14                        SpaceUnit =
15                         TimeUnit =
16                       PrintTimes =
17                  NumberOfSolutes =
18                 InitialCondition =
19                    NumberOfNodes =
20                     ProfileDepth =
21                 ObservationNodes =
22                      GridVisible =
23                       SnapToGrid =
24                     ProfileWidth =
25                       LeftMargin =
26                         GridOrgX =
27                         GridOrgY =
28                           GridDX =
29                           GridDY =

第二个数据帧是

这里 3 代表我创建的后备文件中的行号,我从中获取第 3 行并将其转换为列。这里的值是不同的。问题很简单。我想在等号后写入值并希望写入文本文件。

> C1    
      3
1     4
2     0
3     0
4     0
5     0
6     0
7     0
8     0
9     0
10    0
11    0
12    0
13    0
14   cm
15 days
16  160
17    0
18    1
19  101
20  120
21  160
22  160
23  160
24  160
25  160
26  160
27  160
28  160
29  160

所需的输出文本文件为

HYDRUS_Version=4
WaterFlow=3
SoluteTransport=0
Unsatchem=0
Unsatchem=0
HP1=2
HeatTransport=0
EquilibriumAdsorption=1
MobileImmobile=0
RootWaterUptake=1
RootGrowth=0
MaterialNumbers=1
SubregionNumbers=1
SpaceUnit=cm
TimeUnit=days
PrintTimes=180
NumberOfSolutes=0
InitialCondition=1
NumberOfNodes=101
ProfileDepth=1.2E+02
ObservationNodes=5
GridVisible=1
SnapToGrid=1
ProfileWidth=80
LeftMargin=40
GridOrgX=0
GridOrgY=0

【问题讨论】:

  • 请更正您的问题的格式(尤其是数据格式),以便对用户更加友好(就像它们出现在 R 中一样)
  • 请使用dput()显示您的数据!
  • 可能只是paste 2 列并使用write(..., ncol=1)...
  • > dput(C1) structure(list(3 = structure(c(6L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L, 1L , 7L, 8L, 5L, 1L, 2L, 3L, 4L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L, 5L), .Names = c("V1", "V2", "V3" 、“V4”、“V5”、“V6”、“V7”、“V8”、“V9”、“V10”、“V11”、“V12”、“V13”、“V14”、“V15”、“ V16”、“V17”、“V18”、“V19”、“V20”、“V21”、“V22”、“V23”、“V24”、“V25”、“V26”、“V27”、“V28” , "V29"), .Label = c("0", "1", "101", "120", "160", "4", "cm", "days"), class= "factor") ), .Names = "3", row.names = c(NA, -29L), class= "data.frame")
  • > dput(lfz) 结构(list(readLines("G:/Rlearning/wrds.txt") = 结构(c(9L, 28L, 23L, 27L, 27L, 8L, 7L, 1L, 13L, 21L, 20L, 12L, 25L , 24L, 26L, 17L, 15L, 10L, 14L, 18L, 16L, 6L, 22L, 19L, 11L, 4L, 5L, 2L, 3L), .Label = c("EquilibriumAdsorption=", "GridDX=", " GridDY =","GridOrgX =","GridOrgY =","GridVisible =","HeatTransport =","HP1 =","HYDRUS_Version =","InitialCondition =","LeftMargin =","MaterialNumbers ="," MobileImmobile ="、"NumberOfNodes ="、"NumberOfSolutes ="、

标签: r optimization dataframe text


【解决方案1】:

您只需将字符串粘贴在一起,删除空格,然后将其写入文件:

outVec <- gsub("\\s*", "", paste(lfz[,1], C1[,1])) 
writeLines(outVec, "outfile.txt")   

您的数据应如下所示:

HYDRUS_Version=4
WaterFlow=0
SoluteTransport=0
Unsatchem=0
Unsatchem=0
HP1=0
HeatTransport=0
EquilibriumAdsorption=0
MobileImmobile=0
RootWaterUptake=0
RootGrowth=0
MaterialNumbers=0
SubregionNumbers=0
SpaceUnit=cm
TimeUnit=days
PrintTimes=160
NumberOfSolutes=0
InitialCondition=1
NumberOfNodes=101
ProfileDepth=120
ObservationNodes=160
GridVisible=160
SnapToGrid=160
ProfileWidth=160
LeftMargin=160
GridOrgX=160
GridOrgY=160
GridDX=160
GridDY=160

【讨论】:

  • 谢谢。我需要更多关于如何了解这些字符串的帮助。谢谢。
【解决方案2】:

让我举个例子:

让我们创建两个数据框:

a <- data.frame(col1 = c('a = ','b = ','c = '))
b <- data.frame(col2 = c(1,2,3))

> a
  col1
1 a = 
2 b = 
3 c = 

> b
  col2
1    1
2    2
3    3

让我们将列从 b 复制到 a

a$col2 <- b$col2

让我们连接两列:

a$final <- paste0(a$col1, a$col2)
a$col1 <- NULL
a$col2 <- NULL

  final
1 a = 1
2 b = 2
3 c = 3

让我们把它写到一个文件中

write.csv(a, file = 'output.csv', row.names = FALSE, quote = FALSE)

【讨论】:

  • 也很酷。在此之前我尝试过这样但它不起作用。谢谢。我还需要一个帮助。现在在我的最终文件中,我想在特定位置插入一些字符???我想插入; \n [Main] 在开始,然后在 InitialCondition=1 ;[Profile] 之后,然后保持所有将从 NumberOfNodes=101 开始的内容
  • @MohsinWaqas 您能否编辑您的原始问题以显示您的输出最终应该是什么样子?
猜你喜欢
  • 1970-01-01
  • 2017-09-02
  • 2017-01-01
  • 2021-08-14
  • 2021-09-21
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-02-04
相关资源
最近更新 更多