如何将R中列表中的数据帧的字符更改为数字?

发布于 2025-01-11 05:46:43 字数 598 浏览 0 评论 0原文

我想将列表数据框中的字符向量转换为数字向量。我附上了一个例子。最后的数据看起来像我的列表,我直接导入到 R 中,并且我想将其中的字符转换为数字...

set.seed(94756)

mat1 <- matrix(sample(seq(-1,100, 0.11),50, replace = TRUE),ncol = 5) 
mat1 <- as.data.frame(mat1)
mat1$V2 <- as.character(mat1$V2)

mat2 <- matrix(sample(seq(-1,100, 0.11),50, replace = TRUE),ncol = 5)  
mat2 <- as.data.frame(mat2)
mat2$V2 <- as.character(mat2$V2)

mat3 <- matrix(sample(seq(-1,100, 0.11), 50,replace = TRUE),ncol = 5)  
mat3 <- as.data.frame(mat2)
mat3$V2 <- as.character(mat3$V2)

data <- list(mat1, mat2, mat3)

提前致谢!

I would like to convert a character vector within a dataframe of a list into a numeric one. I have an example attached. Data at the end looks like my list I import directly into R and in which I want to convert character into numeric...

set.seed(94756)

mat1 <- matrix(sample(seq(-1,100, 0.11),50, replace = TRUE),ncol = 5) 
mat1 <- as.data.frame(mat1)
mat1$V2 <- as.character(mat1$V2)

mat2 <- matrix(sample(seq(-1,100, 0.11),50, replace = TRUE),ncol = 5)  
mat2 <- as.data.frame(mat2)
mat2$V2 <- as.character(mat2$V2)

mat3 <- matrix(sample(seq(-1,100, 0.11), 50,replace = TRUE),ncol = 5)  
mat3 <- as.data.frame(mat2)
mat3$V2 <- as.character(mat3$V2)

data <- list(mat1, mat2, mat3)

Thanks in advance!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

_畞蕅 2025-01-18 05:46:43

type.convert 也可以更改 list 中的列类型(假设 data.frame 中的这些列没有任何非数字元素在其中)

data <- type.convert(data, as.is = TRUE)

-输出

> str(data)
List of 3
 $ :'data.frame':   10 obs. of  5 variables:
  ..$ V1: num [1:10] 46.08 10.55 36.62 46.52 5.38 ...
  ..$ V2: num [1:10] 47.2 12.9 38.7 53 84.1 ...
  ..$ V3: num [1:10] 81.9 58.3 75 56.8 57.9 ...
  ..$ V4: num [1:10] 60.5 52.2 77 55.6 95.4 ...
  ..$ V5: num [1:10] 96.5 40.7 58.7 28.3 16.7 ...
 $ :'data.frame':   10 obs. of  5 variables:
  ..$ V1: num [1:10] 14.1 20.9 32.5 91.7 58.7 ...
  ..$ V2: num [1:10] 47.4 48.9 63.1 48 72 ...
  ..$ V3: num [1:10] 61.6 21 56.5 88 53.2 ...
  ..$ V4: num [1:10] 56.86 -0.01 12.86 33.32 55.32 ...
  ..$ V5: num [1:10] 73.58 9.12 57.96 10.88 86.78 ...
 $ :'data.frame':   10 obs. of  5 variables:
  ..$ V1: num [1:10] 14.1 20.9 32.5 91.7 58.7 ...
  ..$ V2: num [1:10] 47.4 48.9 63.1 48 72 ...
  ..$ V3: num [1:10] 61.6 21 56.5 88 53.2 ...
  ..$ V4: num [1:10] 56.86 -0.01 12.86 33.32 55.32 ...
  ..$ V5: num [1:10] 73.58 9.12 57.96 10.88 86.78 ...

type.convert can change column types in list as well (assuming those columns within the data.frame doesn't have any non-numeric element in it)

data <- type.convert(data, as.is = TRUE)

-output

> str(data)
List of 3
 $ :'data.frame':   10 obs. of  5 variables:
  ..$ V1: num [1:10] 46.08 10.55 36.62 46.52 5.38 ...
  ..$ V2: num [1:10] 47.2 12.9 38.7 53 84.1 ...
  ..$ V3: num [1:10] 81.9 58.3 75 56.8 57.9 ...
  ..$ V4: num [1:10] 60.5 52.2 77 55.6 95.4 ...
  ..$ V5: num [1:10] 96.5 40.7 58.7 28.3 16.7 ...
 $ :'data.frame':   10 obs. of  5 variables:
  ..$ V1: num [1:10] 14.1 20.9 32.5 91.7 58.7 ...
  ..$ V2: num [1:10] 47.4 48.9 63.1 48 72 ...
  ..$ V3: num [1:10] 61.6 21 56.5 88 53.2 ...
  ..$ V4: num [1:10] 56.86 -0.01 12.86 33.32 55.32 ...
  ..$ V5: num [1:10] 73.58 9.12 57.96 10.88 86.78 ...
 $ :'data.frame':   10 obs. of  5 variables:
  ..$ V1: num [1:10] 14.1 20.9 32.5 91.7 58.7 ...
  ..$ V2: num [1:10] 47.4 48.9 63.1 48 72 ...
  ..$ V3: num [1:10] 61.6 21 56.5 88 53.2 ...
  ..$ V4: num [1:10] 56.86 -0.01 12.86 33.32 55.32 ...
  ..$ V5: num [1:10] 73.58 9.12 57.96 10.88 86.78 ...
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文