合并到r中的两个数据帧显示< 0行>. (或长度为0的行名) [英] Two dataframes after merging in r are showing <0 rows> (or 0 length row names)

查看:396
本文介绍了合并到r中的两个数据帧显示< 0行>. (或长度为0的行名)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在从Code School学习R,目前在第6章中遇到问题.现在,无论我在这里学到什么,我都可以在桌面上离线实践它.

I'm learning R from Code School and currently doing problems in chapter 6. Now whatever I learn there, I practice it offline in my desktop.

现在,我面临的问题似乎相对非常简单,但是我找不到任何简单的答案.

Now, the problem I am facing seems relatively very simple but I cannot find any easy answer.

在本章中,我们介绍了两个数据集. 两者具有相同的四个端口.

In this chapter, we are introduced with two datasets. Both have the same four ports.

第一个数据集,名为Targets,具有三列(PortPopulationWorth)

First dataset, which is named as Targets have three columns (Port, Population, and Worth)

Port    Population  Worth
Cartagena    35000  10000
Porto Bello  49000  15000
Havana      140000  50000
Panama City 105000  35000

和名为Infantry的第二个数据集有两列(PortInfantry).

and second dataset, which is named as Infantry have two columns (Port, and Infantry).

Port        Infantry
Porto Bello      700
Cartagena        500
Panama City     1500
Havana          2000

两个数据集都保存在两个单独的csv文件中. 现在,在加载它们后

Both dataset are saved in two separate csv files. Now after loading them

Targets = read.csv(file.choose()) 
Infantry = read.csv(file.choose()

每当我尝试通过键入

merge(Targets,Infantry)

结果显示:

[1] Port       Population Worth      Infantry  
<0 rows> (or 0-length row.names)

推荐答案

从您的示例创建一些虚拟数据时,合并功能可以正常工作.

When creating some dummy data from you example, the merge function works fine.

  Targets <- data.frame(
  Port=c("Cartagena", "Porto Bello"),
  Population=c(35000, 49000),
  Worth=c(10000, 15000))

Infantry <- data.frame(
  Port=c("Porto Bello", "Cartagena"), 
  Infantry=c(700, 500))

merge(Targets,Infantry)

您可以检查read.csv功能是否正确上传文件吗?您可能需要在read.csv函数或合并函数的by.xby.y中更改参数sep?

Can you check if the read.csv function uploads the files correctly? You may need to change the argument sep in the read.csv function or by.x and by.y of the merge function?

这篇关于合并到r中的两个数据帧显示&lt; 0行&gt;. (或长度为0的行名)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆