在R中的数据框中进行坐标分配

时间:2021-10-26 19:34:50

I have read in a CSV file in R and want to change one cell in it. Below is the basic idea behind something I've tried, which I found on a website but doesn't work for me.

我在R中的CSV文件中读过并想要更改其中的一个单元格。以下是我尝试过的基本想法,我在网站上找到但不适合我。

data <- read.csv('file.csv')
data[1, 2 := 3]

I've also tried guessing that the way to do it is,

我也试过猜测这样做的方法是,

data[1,2] = 3

or

要么

data[1,2] <- 3

But those don't work either and my googling hasn't turned up anything else, but perhaps I just don't know what to google here. (Tried "coordinate assignment r" and a similar things.)

但那些也不起作用,我的谷歌搜索没有出现任何其他东西,但也许我只是不知道谷歌在这里。 (试过“坐标赋值r”和类似的东西。)

With this code, suppose that the file referenced above by 'file.csv' had the following entries:

使用此代码,假设上面由'file.csv'引用的文件具有以下条目:

0, 1, 2
3, 4, 5

I would want to read this into the variable data and run a command so that when I print data it reads

我想把它读入变量数据并运行一个命令,这样当我打印数据时它会读取

0, 3, 2
3, 4, 5

1 个解决方案

#1


1  

> x <- matrix(c(0,3,1,4,2,5),2,3)
> write.table(x, "data.csv", row.names=FALSE,col.names=FALSE,sep=", ")
> data <- read.csv("data.csv", header=FALSE)
> data
  V1 V2 V3
1  0  1  2
2  3  4  5
> data[1,2] <- 3
> data
  V1 V2 V3
1  0  3  2
2  3  4  5

#1


1  

> x <- matrix(c(0,3,1,4,2,5),2,3)
> write.table(x, "data.csv", row.names=FALSE,col.names=FALSE,sep=", ")
> data <- read.csv("data.csv", header=FALSE)
> data
  V1 V2 V3
1  0  1  2
2  3  4  5
> data[1,2] <- 3
> data
  V1 V2 V3
1  0  3  2
2  3  4  5

相关文章